Filter Restaurants by Vegan-Friendly, Price and Distance
Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei], filter the restaurants using three filters.
The veganFriendly filter will be either:
1, meaning you should only include restaurants withveganFriendlyiset to1.0, meaning you can include any restaurant.
In addition, the filters maxPrice and maxDistance are the maximum values for the price and distance of restaurants you should consider, respectively.
Return the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id from highest to lowest. For simplicity, veganFriendlyi and veganFriendly take value 1 when true and 0 when false.
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[3,1,5]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 = 0, maxPrice = 50, maxDistance = 10[4,3,2,1,5]Constraints
- 1 <= restaurants.length <= 10^4
- restaurants[i].length == 5
- 1 <= idi, ratingi, pricei, distancei <= 10^5
- 1 <= maxPrice, maxDistance <= 10^5
- veganFriendlyi and veganFriendly are 0 or 1.
- All idi are distinct.
Asked at 1 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.