Drop Type 1 Orders for Customers With Type 0 Orders - Problem
Drop Type 1 Orders for Customers With Type 0 Orders
You are working with an e-commerce database that tracks customer orders. Each order has a unique ID, belongs to a customer, and has a specific order type (either 0 or 1).
Your task is to implement a priority filtering system where:
- Type 0 orders have higher priority than Type 1 orders
- If a customer has at least one Type 0 order, we should exclude all their Type 1 orders from the results
- If a customer has only Type 1 orders, we include all their orders
Think of this as a VIP customer system where Type 0 represents premium orders and Type 1 represents regular orders. Once a customer places a premium order, we only show their premium activity.
Goal: Filter the orders table based on the priority rules above and return the filtered results.
Input & Output
example_1.py โ Python
$
Input:
orders = [[1,1,0],[2,1,1],[3,2,1],[4,3,0],[5,3,1]]
โบ
Output:
[[1,1,0],[3,2,1],[4,3,0]]
๐ก Note:
Customer 1 has both Type 0 (order 1) and Type 1 (order 2) orders, so we only include the Type 0 order. Customer 2 has only Type 1 orders, so we include all (order 3). Customer 3 has both types, so we only include Type 0 (order 4).
example_2.py โ Python
$
Input:
orders = [[1,1,1],[2,1,1],[3,2,1]]
โบ
Output:
[[1,1,1],[2,1,1],[3,2,1]]
๐ก Note:
All customers (1 and 2) only have Type 1 orders, so we include all orders in the result.
example_3.py โ Python
$
Input:
orders = [[1,1,0],[2,2,0],[3,3,0]]
โบ
Output:
[[1,1,0],[2,2,0],[3,3,0]]
๐ก Note:
All customers only have Type 0 orders, so we include all orders. Since they don't have Type 1 orders, there's nothing to filter out.
Constraints
- 1 โค orders.length โค 104
- orders[i].length == 3
- 1 โค order_id, customer_id โค 1000
- order_type is either 0 or 1
- order_id is unique for each order
Visualization
Tap to expand
Understanding the Visualization
1
Scan Reservations
Process each reservation and categorize customers by their status
2
Identify VIP Status
Keep track of which customers have made VIP reservations (Type 0)
3
Apply Priority Rules
For VIP customers, show only VIP reservations. For regular customers, show all reservations
Key Takeaway
๐ฏ Key Insight: Use hash table grouping to efficiently categorize customers and apply priority rules in a single pass through the data.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code