LeetCode 1555: Bank Account Summary Solution

Master LeetCode problem 1555 (Bank Account Summary), 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.

1555. Bank Account Summary

Medium

Problem Explanation

Explanation:

To solve this problem, we can iterate through the transactions list and keep track of the balance for each account. We can use a HashMap to store the account balances and then calculate the final balance for each account by considering all the transactions.

  1. Initialize a HashMap to store the balance of each account.
  2. Iterate through the transactions list:
    • Update the balance for the "sender" account by subtracting the transaction amount.
    • Update the balance for the "receiver" account by adding the transaction amount.
  3. After processing all transactions, iterate through the HashMap to construct the final summary.

Time Complexity: O(n), where n is the number of transactions. Space Complexity: O(n), where n is the number of unique accounts.

:

Solution Code

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public Map<Integer, Integer> bankAccountSummary(List<List<Integer>> transactions) {
    Map<Integer, Integer> balances = new HashMap<>();
    
    for (List<Integer> transaction : transactions) {
        int sender = transaction.get(0);
        int receiver = transaction.get(1);
        int amount = transaction.get(2);
        
        balances.put(sender, balances.getOrDefault(sender, 0) - amount);
        balances.put(receiver, balances.getOrDefault(receiver, 0) + amount);
    }
    
    return balances;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1555 (Bank Account Summary)?

This page provides optimized solutions for LeetCode problem 1555 (Bank Account Summary) 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 1555 (Bank Account Summary)?

The time complexity for LeetCode 1555 (Bank Account Summary) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1555 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1555 in Java, C++, or Python.

Back to LeetCode Solutions