Calculate the Influence of Each Salesperson — LeetCode 2372 Python Solution

MediumLeetCode PremiumDatabase
Problem
#2372
Reading time
3 min

Table schema

SQL
Table: Salesperson +----------------+---------+ | Column Name | Type | +----------------+---------+ | salesperson_id | int | | name | varchar | +----------------+---------+ salesperson_id contains unique values. Each row in this table shows the ID of a salesperson.

Example

SQL
+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| salesperson_id | int     |
| name           | varchar |
+----------------+---------+
salesperson_id contains unique values.
Each row in this table shows the ID of a salesperson.

Python solution

Python
import duckdb
import pandas as pd

def solution(salesperson: pd.DataFrame, customer: pd.DataFrame, sales: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Salesperson", salesperson)
    con.register("Customer", customer)
    con.register("Sales", sales)
    return con.execute("""SELECT sp.salesperson_id, name, IFNULL(SUM(price), 0) AS total
FROM
    Salesperson AS sp
    LEFT JOIN Customer AS c ON sp.salesperson_id = c.salesperson_id
    LEFT JOIN Sales AS s ON s.customer_id = c.customer_id
GROUP BY 1;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2372. Calculate the Influence of Each Salesperson?
LeetCode 2372. Calculate the Influence of Each Salesperson is rated Medium on LeetCode.
What topics does LeetCode 2372. Calculate the Influence of Each Salesperson cover?
LeetCode 2372. Calculate the Influence of Each Salesperson is tagged Database on LeetCode.
Is LeetCode 2372. Calculate the Influence of Each Salesperson a premium problem?
Yes. LeetCode 2372. Calculate the Influence of Each Salesperson 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.

Get Stealth Interview