Skip to main content
Back to problems
#1333
Medium Algorithms

Filter restaurants by vegan friendly price and distance

Array Sorting
64.1% acceptance
Feb 25, 2026
320
230
Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You have 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 value 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 it is true, and 0 when it is false.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn filter_restaurants(
    mut restaurants: Vec<Vec<i32>>,
    vegan_friendly: i32,
    max_price: i32,
    max_distance: i32,
  ) -> Vec<i32> {
    restaurants.retain(|r| {
      (vegan_friendly == 0 || r[2] == 1) && r[3] <= max_price && r[4] <= max_distance
    });
    restaurants.sort_by(|a, b| b[1].cmp(&a[1]).then(b[0].cmp(&a[0])));
    restaurants.iter().map(|r| r[0]).collect()
  }
}