Customers With Strictly Increasing Purchases — LeetCode 2474 Python Solution
HardLeetCode PremiumDatabase
- Problem
- #2474
- Reading time
- 4 min
- Source
- leetcode.com
Table schema
SQL
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
SQL
+--------------+------+
| 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
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.