Mid/SeniorArraySorting

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 with veganFriendlyi set to 1.
  • 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.

Example 1
Inputrestaurants = [[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
Output[3,1,5]
After applying veganFriendly = 1, maxPrice = 50, and maxDistance = 10, restaurants 3, 1, and 5 remain and are ordered by rating from highest to lowest.
Example 2
Inputrestaurants = [[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
Output[4,3,2,1,5]
With veganFriendly = 0, all restaurants are considered, then sorted by rating descending and id descending for ties.

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

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate