Filter Restaurants by Vegan-Friendly, Price and Distance — LeetCode #1333 (Medium)
Tags: Array, Sorting
Related patterns: Sorting, Filtering, Array
Brute Force approach
Time complexity: O(n²). Space complexity: O(1).
In the brute-force approach, we will iterate through all the restaurants and check if each one meets the filtering criteria. If it does, we will add it to our result list. This is straightforward but inefficient for larger datasets.
The time complexity is O(n²) because we sort the filtered list, which can take up to O(n log n) time, and the filtering process is O(n). The space complexity is O(1) since we are not using any additional data structures that grow with input size.
- Step 1: Initialize an empty list to store the filtered restaurant IDs.
- Step 2: Loop through each restaurant in the input array.
- Step 3: For each restaurant, check if it meets the vegan-friendly, price, and distance criteria.
- Step 4: If it meets the criteria, add its ID to the result list.
- Step 5: Sort the result list first by rating (descending) and then by ID (descending).
- Step 6: Return the sorted list of IDs.
1. Start with restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10.
2. Check restaurant 1: meets criteria, add ID 1.
3. Check restaurant 2: does not meet vegan-friendly criteria.
4. Check restaurant 3: meets criteria, add ID 3.
5. Check restaurant 4: does not meet vegan-friendly criteria.
6. Check restaurant 5: meets criteria, add ID 5. Result before sorting: [1, 3, 5]. After sorting: [3, 1, 5].
Optimal Solution approach
Time complexity: O(n log n). Space complexity: O(n).
The optimal solution uses a single pass to filter the restaurants and then sorts the filtered results. This is more efficient as it reduces the number of times we loop through the data.
The time complexity is O(n log n) due to the sorting step after filtering, while the filtering itself is O(n). The space complexity is O(n) because we store the filtered results before sorting.
- Step 1: Initialize an empty list to store the filtered restaurant IDs.
- Step 2: Loop through each restaurant and check if it meets the filtering criteria.
- Step 3: If it meets the criteria, add a tuple of (rating, id) to the result list.
- Step 4: Sort the result list by rating (descending) and then by ID (descending).
- Step 5: Extract and return only the IDs from the sorted list.
1. Start with restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]].
2. Check restaurant 1: meets criteria, add (4, 1).
3. Check restaurant 2: does not meet vegan-friendly criteria.
4. Check restaurant 3: meets criteria, add (8, 3).
5. Check restaurant 4: does not meet vegan-friendly criteria.
6. Check restaurant 5: meets criteria, add (1, 5). Result before sorting: [(4, 1), (8, 3), (1, 5)]. After sorting: [(8, 3), (4, 1), (1, 5)]. Final IDs: [3, 1, 5].
Key Insights
- Filtering is crucial before sorting to reduce unnecessary computations.
- Sorting can be done efficiently using built-in functions.
Common Mistakes
- Not considering the vegan-friendly filter correctly.
- Forgetting to sort the results as specified.
Interview Tips
- Always clarify the requirements before coding.
- Think about edge cases, such as empty input or all restaurants being filtered out.
- Discuss your thought process and approach with the interviewer.