LeetCode 1193: Monthly Transactions I Solution
Master LeetCode problem 1193 (Monthly Transactions I), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1193. Monthly Transactions I
Problem Explanation
Explanation:
To solve this problem, we need to group the transactions by month and country. Then, for each group, we need to calculate the total number of transactions, total amount, number of approved transactions, and total amount of approved transactions.
We can achieve this by using the SQL GROUP BY clause along with aggregate functions like COUNT(), SUM(), and CASE WHEN.
The steps to solve the problem are as follows:
- Group the transactions by month and country.
- Count the total number of transactions and their total amount for each group.
- Count the number of approved transactions and their total amount for each group.
- Use CASE WHEN to differentiate between approved and declined transactions.
- Return the result table with the required columns.
The time complexity of this solution is O(n), where n is the number of rows in the Transactions table. The space complexity is also O(n) to store the result set.
:
Solution Code
# Write your Java solution here
String sql = "SELECT DATE_FORMAT(trans_date, '%Y-%m') AS month, country, " +
"COUNT(*) AS trans_count, " +
"SUM(amount) AS trans_total_amount, " +
"SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count, " +
"SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount " +
"FROM Transactions " +
"GROUP BY month, country";Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1193 (Monthly Transactions I)?
This page provides optimized solutions for LeetCode problem 1193 (Monthly Transactions I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1193 (Monthly Transactions I)?
The time complexity for LeetCode 1193 (Monthly Transactions I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1193 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1193 in Java, C++, or Python.