#3705
Find Golden Hour Customers
MediumHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
Use aggregation to compute counts and averages in a single pass, reducing repeated calculations.
⚙️
Algorithm
3 steps- 1Step 1: Aggregate orders by customer to count total orders, rated orders, and sum ratings.
- 2Step 2: Calculate peak hour orders in the same aggregation.
- 3Step 3: Filter customers based on the criteria using HAVING clause.
solution.py5 lines
1# Full working Python code
2SELECT customer_id
3FROM restaurant_orders
4GROUP BY customer_id
5HAVING COUNT(order_id) >= 3 AND AVG(order_rating) >= 4.0 AND SUM(CASE WHEN (HOUR(order_timestamp) BETWEEN 11 AND 14 OR HOUR(order_timestamp) BETWEEN 18 AND 21) THEN 1 ELSE 0 END) / COUNT(order_id) >= 0.6 AND COUNT(order_rating) / COUNT(order_id) >= 0.5;ℹ
Complexity note: Single pass through data for aggregation leads to linear time complexity.
- 1Focus on aggregating data efficiently.
- 2Understand the importance of filtering based on multiple criteria.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.