LeetCode 1333. 餐厅过滤器

【LetMeFly】1333.餐厅过滤器

力扣题目链接:https://leetcode.cn/problems/filter-restaurants-by-vegan-friendly-price-and-distance/

给你一个餐馆信息数组 restaurants,其中  restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]。你必须使用以下三个过滤器来过滤这些餐馆信息。

其中素食者友好过滤器 veganFriendly 的值可以为 true 或者 false,如果为 true 就意味着你应该只包括 veganFriendlyi 为 true 的餐馆,为 false 则意味着可以包括任何餐馆。此外,我们还有最大价格 maxPrice 和最大距离 maxDistance 两个过滤器,它们分别考虑餐厅的价格因素和距离因素的最大值。

过滤后返回餐馆的 id,按照 rating 从高到低排序。如果 rating 相同,那么按 id 从高到低排序。简单起见, veganFriendlyiveganFriendly 为 true 时取值为 1,为 false 时,取值为 0 。

示例 1:

输入: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
输出:[3,1,5] 
解释: 
这些餐馆为:
餐馆 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
餐馆 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
餐馆 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
餐馆 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
餐馆 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1] 
在按照 veganFriendly = 1, maxPrice = 50 和 maxDistance = 10 进行过滤后,我们得到了餐馆 3, 餐馆 1 和 餐馆 5(按评分从高到低排序)。 

示例 2:

输入: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
输出:[4,3,2,1,5]
解释:餐馆与示例 1 相同,但在 veganFriendly = 0 的过滤条件下,应该考虑所有餐馆。

示例 3:

输入: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
输出:[4,5]

提示:

  • 1 <= restaurants.length <= 10^4
  • restaurants[i].length == 5
  • 1 <= idi, ratingi, pricei, distancei <= 10^5
  • 1 <= maxPrice, maxDistance <= 10^5
  • veganFriendlyi 和 veganFriendly 的值为 0 或 1 。
  • 所有 idi 各不相同。

方法一:排序

建立一个临时数组,数组中存放满足条件的元素。

接着将数组中的元素按照rating优先其次id优先的规则排序,排序后,将临时数组中每个元素的id放入新数组并返回即可。

怎么判断某个餐厅是否满足条件呢?

  • veganFriendly:如果这个餐厅的veganFriendly大于等于所需的veganFriendly,则不删
  • price:如果这个餐厅的price大于等于所需的price,则不删
  • distance:如果这个餐厅的distance大于等于所需的distance,则不删

即可。

  • 时间复杂度 O ( n log ⁡ n ) O(n\log n) O(nlogn),其中 n = l e n ( r e s t a u r a n t s ) n = len(restaurants) n=len(restaurants)
  • 空间复杂度 O ( n ) O(n) O(n)

AC代码

C++
class Solution {
    
    
public:
    vector<int> filterRestaurants(vector<vector<int>>& restaurants, int veganFriendly, int maxPrice, int maxDistance) {
    
    
        vector<vector<int>> temp;
        for (auto&& v : restaurants) {
    
    
            if (v[2] >= veganFriendly && v[3] <= maxPrice && v[4] <= maxDistance) {
    
    
                temp.push_back(v);
            }
        }
        sort(temp.begin(), temp.end(), [](const vector<int>& a, const vector<int>& b) {
    
    
            return a[1] == b[1] ? a[0] > b[0] : a[1] > b[1];
        });
        vector<int> ans;
        for (auto&& v : temp) {
    
    
            ans.push_back(v[0]);
        }
        return ans;
    }
};
Python
# from typing import List

class Solution:
    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:
        temp = [v for v in restaurants if v[2] >= veganFriendly and v[3] <= maxPrice and v[4] <= maxDistance]
        temp.sort(key=lambda v:(-v[1], -v[0]))
        return [v[0] for v in temp]

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/133362152

猜你喜欢

转载自blog.csdn.net/Tisfy/article/details/133362152
今日推荐