Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
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] Explanation: The restaurants are: Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10] Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5] Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4] Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3] Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] After filter restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest).
Example 2:
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] Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
Example 3:
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 = 30, maxDistance = 3 Output: [4,5]
Constraints:
1 <= restaurants.length <= 10^4restaurants[i].length == 51 <= idi, ratingi, pricei, distancei <= 10^51 <= maxPrice, maxDistance <= 10^5veganFriendlyi and veganFriendly are 0 or 1.idi are distinct.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[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]] 1 50 10
[[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]] 0 50 10
[[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]] 0 30 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1333: Filter Restaurants by Vegan-Friendly, Price and Distance
class Solution {
public List<Integer> filterRestaurants(
int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
Arrays.sort(restaurants, (a, b) -> a[1] == b[1] ? b[0] - a[0] : b[1] - a[1]);
List<Integer> ans = new ArrayList<>();
for (int[] r : restaurants) {
if (r[2] >= veganFriendly && r[3] <= maxPrice && r[4] <= maxDistance) {
ans.add(r[0]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #1333: Filter Restaurants by Vegan-Friendly, Price and Distance
func filterRestaurants(restaurants [][]int, veganFriendly int, maxPrice int, maxDistance int) (ans []int) {
sort.Slice(restaurants, func(i, j int) bool {
a, b := restaurants[i], restaurants[j]
if a[1] != b[1] {
return a[1] > b[1]
}
return a[0] > b[0]
})
for _, r := range restaurants {
if r[2] >= veganFriendly && r[3] <= maxPrice && r[4] <= maxDistance {
ans = append(ans, r[0])
}
}
return
}
# Accepted solution for LeetCode #1333: Filter Restaurants by Vegan-Friendly, Price and Distance
class Solution:
def filterRestaurants(
self,
restaurants: List[List[int]],
veganFriendly: int,
maxPrice: int,
maxDistance: int,
) -> List[int]:
restaurants.sort(key=lambda x: (-x[1], -x[0]))
ans = []
for idx, _, vegan, price, dist in restaurants:
if vegan >= veganFriendly and price <= maxPrice and dist <= maxDistance:
ans.append(idx)
return ans
// Accepted solution for LeetCode #1333: Filter Restaurants by Vegan-Friendly, Price and Distance
struct Solution;
use std::cmp::Reverse;
type Pair = (Reverse<i32>, Reverse<i32>);
impl Solution {
fn filter_restaurants(
restaurants: Vec<Vec<i32>>,
vegan_friendly: i32,
max_price: i32,
max_distance: i32,
) -> Vec<i32> {
let mut pairs: Vec<Pair> = restaurants
.into_iter()
.filter_map(|r| {
if (vegan_friendly == 0 || r[2] == 1) && r[3] <= max_price && r[4] <= max_distance {
Some((Reverse(r[1]), Reverse(r[0])))
} else {
None
}
})
.collect();
pairs.sort_unstable();
pairs.into_iter().map(|p| (p.1).0).collect()
}
}
#[test]
fn test() {
let restaurants = vec_vec_i32![
[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]
];
let vegan_friendly = 1;
let max_price = 50;
let max_distance = 10;
let res = vec![3, 1, 5];
assert_eq!(
Solution::filter_restaurants(restaurants, vegan_friendly, max_price, max_distance),
res
);
let restaurants = vec_vec_i32![
[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]
];
let vegan_friendly = 0;
let max_price = 50;
let max_distance = 10;
let res = vec![4, 3, 2, 1, 5];
assert_eq!(
Solution::filter_restaurants(restaurants, vegan_friendly, max_price, max_distance),
res
);
let restaurants = vec_vec_i32![
[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]
];
let vegan_friendly = 0;
let max_price = 30;
let max_distance = 3;
let res = vec![4, 5];
assert_eq!(
Solution::filter_restaurants(restaurants, vegan_friendly, max_price, max_distance),
res
);
}
// Accepted solution for LeetCode #1333: Filter Restaurants by Vegan-Friendly, Price and Distance
function filterRestaurants(
restaurants: number[][],
veganFriendly: number,
maxPrice: number,
maxDistance: number,
): number[] {
restaurants.sort((a, b) => (a[1] === b[1] ? b[0] - a[0] : b[1] - a[1]));
const ans: number[] = [];
for (const [id, _, vegan, price, distance] of restaurants) {
if (vegan >= veganFriendly && price <= maxPrice && distance <= maxDistance) {
ans.push(id);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.