Daily Leads and Partners — LeetCode 1693 Python Solution

EasyDatabase
Problem
#1693
Reading time
3 min

Table schema

SQL
Table: DailySales +-------------+---------+ | Column Name | Type | +-------------+---------+ | date_id | date | | make_name | varchar | | lead_id | int | | partner_id | int | +-------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates.

Example

SQL
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| date_id     | date    |
| make_name   | varchar |
| lead_id     | int     |
| partner_id  | int     |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
This table contains the date and the name of the product sold and the IDs of the lead and partner it was sold to.
The name consists of only lowercase English letters.

Python solution

Python
import duckdb
import pandas as pd

def solution(daily_sales: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("DailySales", daily_sales)
    return con.execute("""SELECT
    date_id,
    make_name,
    COUNT(DISTINCT lead_id) AS unique_leads,
    COUNT(DISTINCT partner_id) AS unique_partners
FROM DailySales
GROUP BY 1, 2;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1693. Daily Leads and Partners?
LeetCode 1693. Daily Leads and Partners is rated Easy on LeetCode.
What topics does LeetCode 1693. Daily Leads and Partners cover?
LeetCode 1693. Daily Leads and Partners is tagged Database on LeetCode.

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