Top Travellers — LeetCode #1407 (Easy)
Tags: Database
Related patterns: Hash Map, Aggregation Functions
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
In this approach, we will calculate the total distance traveled by each user by iterating through the Rides table for each user in the Users table. This is straightforward but inefficient as it involves nested loops.
This complexity arises because for each user, we are iterating through all rides, leading to a nested loop structure.
- Step 1: Initialize a dictionary to store the total distance for each user.
- Step 2: For each user in the Users table, iterate through the Rides table to sum their distances.
- Step 3: Store the total distance in the dictionary with the user's name as the key.
- Step 4: Convert the dictionary to a list of tuples for sorting.
- Step 5: Sort the list first by distance (descending) and then by name (ascending).
- Step 6: Return the sorted list.
1. Users: [{1, 'Alice'}, {2, 'Bob'}, {3, 'Alex'}, {4, 'Donald'}, {7, 'Lee'}, {13, 'Jonathan'}, {19, 'Elvis'}]
2. Rides: [{1, 1, 120}, {2, 2, 317}, {3, 3, 222}, {4, 7, 100}, {5, 13, 312}, {6, 19, 50}, {7, 7, 120}, {8, 19, 400}, {9, 7, 230}]
3. Total distances: {1: 120, 2: 317, 3: 222, 4: 0, 7: 450, 13: 312, 19: 450}
4. Convert to list: [('Alice', 120), ('Bob', 317), ('Alex', 222), ('Donald', 0), ('Lee', 450), ('Jonathan', 312), ('Elvis', 450)]
5. Sort: [('Elvis', 450), ('Lee', 450), ('Bob', 317), ('Jonathan', 312), ('Alex', 222), ('Alice', 120), ('Donald', 0)]
6. Final output: [('Elvis', 450), ('Lee', 450), ('Bob', 317), ('Jonathan', 312)]
Optimal Solution approach
Time complexity: O(n). Space complexity: O(n).
By using SQL's aggregation functions effectively, we can compute the total distance for each user in a single pass through the Rides table, which is much more efficient than the brute-force method.
This complexity is due to a single pass through the Rides table to compute the total distances, making it much more efficient than the brute-force approach.
- Step 1: Use a JOIN operation to combine Users and Rides tables on user_id.
- Step 2: Use SUM() to calculate the total distance for each user.
- Step 3: Use GROUP BY to group the results by user id.
- Step 4: Use ORDER BY to sort the results first by total distance (descending) and then by name (ascending).
- Step 5: Return the final result set.
1. Users: [{1, 'Alice'}, {2, 'Bob'}, {3, 'Alex'}, {4, 'Donald'}, {7, 'Lee'}, {13, 'Jonathan'}, {19, 'Elvis'}]
2. Rides: [{1, 1, 120}, {2, 2, 317}, {3, 3, 222}, {4, 7, 100}, {5, 13, 312}, {6, 19, 50}, {7, 7, 120}, {8, 19, 400}, {9, 7, 230}]
3. Total distances calculated: {1: 120, 2: 317, 3: 222, 4: 0, 7: 450, 13: 312, 19: 450}
4. Convert to list: [('Alice', 120), ('Bob', 317), ('Alex', 222), ('Donald', 0), ('Lee', 450), ('Jonathan', 312), ('Elvis', 450)]
5. Sort: [('Elvis', 450), ('Lee', 450), ('Bob', 317), ('Jonathan', 312), ('Alex', 222), ('Alice', 120), ('Donald', 0)]
6. Final output: [('Elvis', 450), ('Lee', 450), ('Bob', 317), ('Jonathan', 312)]
Key Insights
- Using aggregation functions like SUM() can significantly reduce computation time.
- LEFT JOIN ensures all users are included even if they have no rides.
Common Mistakes
- Forgetting to handle users with no rides, which can lead to incorrect results.
- Not ordering the results correctly as per the requirements.
Interview Tips
- Always clarify the requirements before starting to code.
- Think about edge cases, such as users with no rides.
- Practice writing SQL queries to get comfortable with JOINs and GROUP BY.