LeetCode 1333: Filter Restaurants by Vegan-Friendly, Price and Distance Solution
Master LeetCode problem 1333 (Filter Restaurants by Vegan-Friendly, Price and Distance), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
Problem Explanation
Explanation
To solve this problem, we need to iterate through the given array of restaurants and filter out the ones that meet the given criteria: veganFriendly, maxPrice, and maxDistance. We will then sort the remaining restaurants based on their rating (from highest to lowest) and if two restaurants have the same rating, we will order them by id from highest to lowest.
Algorithm
- Iterate through each restaurant in the input array.
- Check if the restaurant satisfies the filtering criteria (veganFriendly, maxPrice, maxDistance).
- Store the restaurant ID along with its rating in a map if it passes the filtering criteria.
- Sort the restaurants based on rating (descending) and then by ID (descending).
- Return the IDs of the filtered and sorted restaurants.
Time Complexity
The time complexity of this algorithm is O(n log n) where n is the number of restaurants in the input array.
Space Complexity
The space complexity of this algorithm is O(n) to store the filtered restaurants.
Solution Code
import java.util.*;
class Solution {
public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
Map<Integer, Integer> filteredRestaurants = new HashMap<>();
for (int[] restaurant : restaurants) {
int id = restaurant[0];
int rating = restaurant[1];
int isVeganFriendly = restaurant[2];
int price = restaurant[3];
int distance = restaurant[4];
if ((veganFriendly == 0 || isVeganFriendly == veganFriendly) && price <= maxPrice && distance <= maxDistance) {
filteredRestaurants.put(id, rating);
}
}
List<Integer> result = new ArrayList<>(filteredRestaurants.keySet());
Collections.sort(result, (a, b) -> {
if (filteredRestaurants.get(a).equals(filteredRestaurants.get(b))) {
return b - a;
}
return filteredRestaurants.get(b) - filteredRestaurants.get(a);
});
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1333 (Filter Restaurants by Vegan-Friendly, Price and Distance)?
This page provides optimized solutions for LeetCode problem 1333 (Filter Restaurants by Vegan-Friendly, Price and Distance) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1333 (Filter Restaurants by Vegan-Friendly, Price and Distance)?
The time complexity for LeetCode 1333 (Filter Restaurants by Vegan-Friendly, Price and Distance) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1333 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1333 in Java, C++, or Python.