Customer Placing the Largest Number of Orders — LeetCode 586 Python Solution
EasyDatabase
- Problem
- #586
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Find the customer who placed the most orders and report their customer number. The test data guarantees exactly one customer holds the record. The Orders table has one row per order: order_number (int, the primary key) and customer_number (int, the customer who placed that order).
Example
Orders table: | order_number | customer_number | | ------------ | --------------- | | 101 | 7 | | 102 | 4 | | 103 | 7 | | 104 | 9 | | 105 | 7 | Result: | customer_number | | --------------- | | 7 | Customer 7 placed three orders while customers 4 and 9 placed one each, so 7 is the answer.
Python solution
Python
import pandas as pd
def customer_with_most_orders(orders: pd.DataFrame) -> pd.DataFrame:
counts = orders.groupby('customer_number').size()
max_cnt = counts.max()
winner = counts[counts == max_cnt].index.min()
return pd.DataFrame({'customer_number': [int(winner)]})Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 586. Customer Placing the Largest Number of Orders?
- LeetCode 586. Customer Placing the Largest Number of Orders is rated Easy on LeetCode.
- What topics does LeetCode 586. Customer Placing the Largest Number of Orders cover?
- LeetCode 586. Customer Placing the Largest Number of Orders is tagged Database on LeetCode.