首页 > 其他分享 >[LeetCode] 1333. Filter Restaurants by Vegan-Friendly, Price and Distance 餐厅过滤器

[LeetCode] 1333. Filter Restaurants by Vegan-Friendly, Price and Distance 餐厅过滤器

时间:2023-02-27 09:24:23浏览次数:66  
标签:Distance rating 10 Price veganFriendly distance Vegan price restaurants


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^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.

这道题给了一个餐馆数组,每个餐馆包括一系列信息,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/

https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/solutions/490344/java-one-liner/

https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/solutions/492399/c-short-solution/


LeetCode All in One 题目讲解汇总(持续更新中...)

标签:Distance,rating,10,Price,veganFriendly,distance,Vegan,price,restaurants
From: https://www.cnblogs.com/grandyang/p/17158526.html

相关文章

  • 兼容oracle的edit_distance_similarity 比较两个字符串相似度
    瀚高数据库目录环境症状问题原因解决方案报错编码环境系统平台:Linuxx86RedHatEnterpriseLinux6版本:4.5.7症状在进行应用适配过程中会遇到用户使用oracle的SYS.UTL_MAT......
  • BZOJ 4145 [AMPPZ2014]The Prices (状压DP)
    题意你要购买商店,你到第i家商店的路费为,在第家商店购买第种物品的费用为,求最小总费用。分析很容易定义出状态,表示到第行,买的物品情况为的最小费用。按照往常的套路是转移时......
  • CF #727(div2)D. PriceFixed, 贪心,双指针
    problemD.PriceFixedtimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputLenaisthemosteconomicalgirli......
  • 2019年ICPC南昌网络赛 J. Distance on the tree(树链剖分+主席树 查询路径边权第k大)
    DSM(DataStructureMaster)oncelearnedabouttreewhenhewaspreparingforNOIP(NationalOlympiadinInformaticsinProvinces)inSeniorHighSchool.Sowhen......
  • Android百度地图sdk 踩坑DistanceUtil.getDistance报错
    计算百度地图两个经纬度的距离一直崩溃,一直在报这个错误,一直在报找不到jni库函数的错误java.lang.UnsatisfiedLinkError:Noimplementationfoundforbooleancom.baid......
  • Hamming Distance汉明距离
    汉明距离是使用在数据传输差错控制编码里面的,汉明距离是一个概念,它表示两个(相同长度)字对应位不同的数量,我们以d(x,y)表示两个字x,y之间的汉明距离。对两个字符串进行异或运算,......
  • ¥price
      Object.assign() 用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。用法:Object.assign(target,...sources);参数:target--->目标对象s......
  • Difference Between Maximum and Minimum Price Sum
    DifferenceBetweenMaximumandMinimumPriceSumThereexistsanundirectedandinitiallyunrootedtreewith$n$nodesindexedfrom$0$to$n-1$.Youaregiv......
  • F - Permutation Distance -- ATCODER
    F-PermutationDistancehttps://atcoder.jp/contests/abc283/tasks/abc283_f 思路最小生成树法:https://zhuanlan.zhihu.com/p/595421879 动态缩减查找距离法朴......
  • abc283 F - Permutation Distance
    题意:给定\(1\simn\)的排列\(P\),对每个\(i\in[1,n]\),计算\(\min\limits_{j\neqi}\{|P_i-P_j|+|i-j|\}\)\(n\le2e5\)思路:我超,俩绝对值怎么办?硬拆就完事了!\[......