Filter Restaurants by Vegan-Friendly, Price and Distance - Problem

Given an array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You need to filter the restaurants using three filters:

The veganFriendly filter will be either true (meaning you should only include restaurants with veganFriendlyi set to true) or false (meaning you can include any restaurant).

In addition, you have the filters maxPrice and maxDistance which are the maximum values for 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.

Input & Output

Example 1 — Basic Filtering
$ Input: 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
Output: [3,1,5]
💡 Note: Filter vegan-friendly restaurants: [1,4,1,40,10], [3,8,1,30,4], [5,1,1,15,1]. Sort by rating desc then id desc: rating 8 (id=3), rating 4 (id=1), rating 1 (id=5) → [3,1,5]
Example 2 — No Vegan Filter
$ Input: 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
Output: [4,3,2,1,5]
💡 Note: Include all restaurants within price/distance limits. Sort by rating desc then id desc: rating 10 (id=4), rating 8 (id=3,2 → 3>2), rating 4 (id=1), rating 1 (id=5)
Example 3 — Strict Distance Filter
$ Input: restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4]], veganFriendly = 0, maxPrice = 50, maxDistance = 3
Output: []
💡 Note: No restaurants within distance 3 (minimum distance is 4), so return empty array

Constraints

  • 1 ≤ restaurants.length ≤ 104
  • restaurants[i].length == 5
  • 1 ≤ idi, ratingi, pricei, distancei ≤ 105
  • 1 ≤ maxPrice, maxDistance ≤ 105
  • veganFriendlyi and veganFriendly are 0 or 1

Visualization

Tap to expand
Filter Restaurants: Complete ProcessInput 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]Filters: vegan=1, maxPrice=50, maxDistance=10After Filtering:[1,4][3,8][5,1]Sort by rating desc, then id desc:Result: [3,1,5]Rating 8 (ID 3) → Rating 4 (ID 1) → Rating 1 (ID 5)
Understanding the Visualization
1
Input
Array of restaurants with [id, rating, vegan, price, distance]
2
Filter
Apply vegan-friendly, price and distance constraints
3
Sort
Sort by rating desc, then by id desc
4
Output
Return array of restaurant IDs
Key Takeaway
🎯 Key Insight: Combine multiple filter conditions and use custom comparator for multi-level sorting
Asked in
DoorDash 25 Uber 20 Amazon 15
15.2K Views
Medium Frequency
~15 min Avg. Time
489 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen