Similar Problems

Similar Problems not available

Bank Account Summary Ii - Leetcode Solution

Companies:

LeetCode:  Bank Account Summary Ii Leetcode Solution

Difficulty: Easy

Topics: database  

Problem Statement:

Given a list of transactions, each transaction including an order number, a transaction ID, the amount of the transaction, and a transaction type.

Return the current balance of the bank account.

The transaction types are:

  • "START" - A new account was started with the given order number.
  • "END" - The account that matches the order number was closed.
  • "DEPOSIT" - A deposit of the given amount was made to the account that matches the order number.
  • "WITHDRAW" - A withdrawal of the given amount was made from the account that matches the order number.

Example: Input:

transactions = [[1,"START",0], [1,"DEPOSIT",100], [2,"START",0], [2,"DEPOSIT",200], [2,"WITHDRAW",50], [1,"WITHDRAW",50], [1,"END",0], [2,"WITHDRAW",150], [2,"END",0]]

Output: [150, 50]

Explanation: For the order number 1, the balance is initially 0. A deposit of 100 is made, bringing the balance to 100. A withdrawal of 50 is made, bringing the balance to 50. Finally, the account is closed, making the balance 0 again. For the order number 2, the balance is initially 0. A deposit of 200 is made, bringing the balance to 200. A withdrawal of 50 is made, bringing the balance to 150. Finally, the account is closed, making the balance 0 again.

Approach:

The given problem can be solved using a dictionary and iterating over each transaction. In the dictionary, we'll store the balance of each account with its order number as the key. We'll update it for each transaction according to its type:

  1. If transaction type is "START", we'll add a new entry to the dictionary with the order number as the key and 0 as the balance.
  2. If transaction type is "DEPOSIT", we'll add the amount to the current balance of the account.
  3. If transaction type is "WITHDRAW", we'll subtract the amount from the current balance of the account.
  4. If transaction type is "END", we'll remove the entry for the account from the dictionary.

Finally, we'll convert the dictionary into a list of balances corresponding to each order number.

Solution:

def bank_account_summary(transactions): # Initializing an empty dictionary to store balances account_balances = {}

# Iterating over each transaction
for transaction in transactions:
    order_number = transaction[0]
    transaction_type = transaction[1]
    amount = transaction[2]

    # For each transaction type, updating the balance of the account
    if transaction_type == "START":
        account_balances[order_number] = 0
    elif transaction_type == "DEPOSIT":
        account_balances[order_number] += amount
    elif transaction_type == "WITHDRAW":
        account_balances[order_number] -= amount
    elif transaction_type == "END":
        del account_balances[order_number]

# Converting the dictionary into a list of balances
balances = []
for order_number in sorted(account_balances.keys()):
    balances.append(account_balances[order_number])
return balances

Testing the function with an example input

transactions = [[1,"START",0], [1,"DEPOSIT",100], [2,"START",0], [2,"DEPOSIT",200], [2,"WITHDRAW",50], [1,"WITHDRAW",50], [1,"END",0], [2,"WITHDRAW",150], [2,"END",0]]

print(bank_account_summary(transactions))

Output: [150, 50]

Bank Account Summary Ii Solution Code

1