HardNo. 2474LeetCode Premium
Customers With Strictly Increasing Purchases — LeetCode 2474 Python Solution
- Topics
- Database
- Reading time
- 4 min
- Source
- leetcode.com
Table schema
Table: Orders +--------------+------+ | Column Name | Type | +--------------+------+ | order_id | int | | customer_id | int | | order_date | date | | price | int | +--------------+------+ order_id is the column with unique values for this table. Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.Example
+--------------+------+
| Column Name | Type |
+--------------+------+
| order_id | int |
| customer_id | int |
| order_date | date |
| price | int |
+--------------+------+
order_id is the column with unique values for this table.
Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price.Python solution
import duckdb
import pandas as pd
def solution(orders: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Orders", orders)
return con.execute("""SELECT
customer_id
FROM
(
SELECT
customer_id,
YEAR(order_date),
SUM(price) AS total,
YEAR(order_date) - RANK() OVER (
PARTITION BY customer_id
ORDER BY SUM(price)
) AS rk
FROM Orders
GROUP BY customer_id, YEAR(order_date)
) AS t
GROUP BY customer_id
HAVING COUNT(DISTINCT rk) = 1;""").df()Complexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
Frequently asked questions
- How hard is LeetCode 2474. Customers With Strictly Increasing Purchases?
LeetCode 2474. Customers With Strictly Increasing Purchases is rated Hard on LeetCode.
- What topics does LeetCode 2474. Customers With Strictly Increasing Purchases cover?
LeetCode 2474. Customers With Strictly Increasing Purchases is tagged Database on LeetCode.
- Is LeetCode 2474. Customers With Strictly Increasing Purchases a premium problem?
Yes. LeetCode 2474. Customers With Strictly Increasing Purchases is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.