MediumNo. 1555LeetCode Premium

Bank Account Summary — LeetCode 1555 Python Solution

Topics
Database
Reading time
4 min

Table schema

SQLTable schema
Table: Users +--------------+---------+ | Column Name | Type | +--------------+---------+ | user_id | int | | user_name | varchar | | credit | int | +--------------+---------+ user_id is the primary key (column with unique values) for this table. Each row of this table contains the current credit information for each user.

Example

SQLExample
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| user_id      | int     |
| user_name    | varchar |
| credit       | int     |
+--------------+---------+
user_id is the primary key (column with unique values) for this table.
Each row of this table contains the current credit information for each user.

Python solution

PythonSolution
import duckdb
import pandas as pd

def solution(users: pd.DataFrame, transactions: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Users", users)
    con.register("Transactions", transactions)
    return con.execute("""SELECT
    t.user_id,
    user_name,
    SUM(t.credit) AS credit,
    IF(SUM(t.credit) < 0, 'Yes', 'No') AS credit_limit_breached
FROM
    (
        SELECT paid_by AS user_id, -amount AS credit FROM Transactions
        UNION ALL
        SELECT paid_to AS user_id, amount AS credit FROM Transactions
        UNION ALL
        SELECT user_id, credit FROM Users
    ) AS t
    JOIN Users AS u ON t.user_id = u.user_id
GROUP BY t.user_id;""").df()

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

Frequently asked questions

How hard is LeetCode 1555. Bank Account Summary?

LeetCode 1555. Bank Account Summary is rated Medium on LeetCode.

What topics does LeetCode 1555. Bank Account Summary cover?

LeetCode 1555. Bank Account Summary is tagged Database on LeetCode.

Is LeetCode 1555. Bank Account Summary a premium problem?

Yes. LeetCode 1555. Bank Account Summary is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.