Leetcode 1333: Filter Restaurants by Vegan-Friendly, Price and Distance

grid47
grid47
Exploring patterns and algorithms
Jun 26, 2024 5 min read

You are given a list of restaurants where each restaurant is represented by an array: [id, rating, veganFriendly, price, distance]. Filter these restaurants based on three criteria: vegan friendliness, maximum price, and maximum distance. Return the restaurant IDs, ordered by rating (highest first), and by ID (highest first) when ratings are tied.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of lists where each inner list represents a restaurant. The structure of each inner list is [id, rating, veganFriendly, price, distance]. Three additional parameters are provided: veganFriendly (1 or 0), maxPrice, and maxDistance.
Example: [[1,5,1,40,10], [2,8,0,50,5], [3,7,1,20,3], [4,9,0,15,6], [5,2,1,25,7]]
Constraints:
• 1 <= restaurants.length <= 10^4
• restaurants[i].length == 5
• 1 <= id, rating, price, distance <= 10^5
• 1 <= maxPrice, maxDistance <= 10^5
• veganFriendly is 0 or 1
• All ids are distinct.
Output: The output is a list of restaurant IDs after applying the filters, sorted by rating in descending order and by ID in descending order for restaurants with the same rating.
Example: [4,2,3,1,5]
Constraints:
Goal: The goal is to filter the restaurants based on vegan friendliness, maximum price, and maximum distance. Then, sort the remaining restaurants by rating and ID.
Steps:
• Loop through each restaurant and apply the filters based on veganFriendly, maxPrice, and maxDistance.
• After filtering, sort the remaining restaurants based on their rating (highest to lowest) and ID (highest to lowest).
Goal:
Steps:
Assumptions:
• Restaurants with the same rating should be sorted by their ID.
• All restaurant IDs are unique.
Input: [[1,5,1,40,10], [2,8,0,50,5], [3,7,1,20,3], [4,9,0,15,6], [5,2,1,25,7]]
Explanation: This example demonstrates filtering based on vegan friendliness and maximum price and distance. After filtering, restaurants are sorted by rating (highest first) and ID (highest first).

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus