[LeetCode] 1333. Filter Restaurants by Vegan-Friendly, Price and Distance 餐廳過濾器
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^5veganFriendlyiandveganFriendlyare 0 or 1.- All
idiare distinct.
這道題給了一個餐館數組,每個餐館包括一系列信息,id,rating,veganFriendly,price,distance。現在給了一些搜索條件,比如是否素食友好,最大價格,最大距離,然后返回符合要求的餐館。這其實就是類似 Yelp 的功能啊,這里的素食友好的條件需要特別注意一下,即素食主義者不能去非素食餐館,而非素食主義者可以去素食餐館,所以不能簡單的對比 veganFriendly。所以當餐館的 veganFriendly 是1,給定的 veganFriendly 是0的話,就不能選擇該餐館。那么取個反,當餐館的 veganFriendly 為1,或者給定的 veganFriendly 為0時可以選餐館,另外餐館的價格要小于等于最大價格,且餐館的距離要小于等于最大距離,將符合要求的餐館選出來放到一個新的數組中。之后就是給這個數組排序,排序的方法就是若 rating 不相等,把大的排前面,若 rating 相等,把 Id 大的放前面即可,參見代碼如下:
class Solution {
public:
vector<int> filterRestaurants(vector<vector<int>>& restaurants, int veganFriendly, int maxPrice, int maxDistance) {
vector<int> res;
vector<vector<int>> filtered;
for (auto r : restaurants) {
if ((r[2] != 0 || veganFriendly != 1) && r[3] <= maxPrice && r[4] <= maxDistance) {
filtered.push_back(r);
}
}
sort(filtered.begin(), filtered.end(), [](vector<int>& a, vector<int>& b) {
return (a[1] != b[1]) ? (a[1] > b[1]) : (a[0] > b[0]);
});
for (auto r : filtered) {
res.push_back(r[0]);
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/1333
參考資料:
https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/


浙公網安備 33010602011771號