​LeetCode解法汇总1333. 餐厅过滤器​

news/2024/7/1 11:34:51

目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台


描述:

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

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

过滤后返回餐馆的 id,按照 rating 从高到低排序。如果 rating 相同,那么按 id 从高到低排序。简单起见, veganFriendlyi 和 veganFriendly 为 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 各不相同。

解题思路:

* 解题思路:

* 首先根据isVeganFriendly,maxPriceThreshold,maxDistanceThreshold进行过滤。

* 过滤完成后,按照restaurants[1]排序,如果restaurants[1]相同则按照restaurants[0]排序。

代码:

class Solution {
public:
    vector<int> filterRestaurants(vector<vector<int>> &restaurants, int isVeganFriendly, int maxPriceThreshold, int maxDistanceThreshold)
    {
        vector<vector<int>> candidates;
        for (vector<int> restaurant : restaurants)
        {
            if (isVeganFriendly == 1 && restaurant[2] == 0)
            {
                continue;
            }
            if (restaurant[3] > maxPriceThreshold)
            {
                continue;
            }
            if (restaurant[4] > maxDistanceThreshold)
            {
                continue;
            }
            candidates.push_back(restaurant);
        }

        sort(candidates.begin(), candidates.end(), [](vector<int> v1, vector<int> v2)
             {
        if (v1[1] != v2[1])
        {
             return v1[1]>v2[1];
        }
        return v1[0] > v2[0]; });

        vector<int> result;
        for (vector<int> restaurant : candidates)
        {
            result.push_back(restaurant[0]);
        }

        return result;
    }
};


http://lihuaxi.xjx100.cn/news/1577231.html

相关文章

力扣(LeetCode)1333. 餐厅过滤器(C++)

优先队列 请读者读题&#xff0c;应该会发现两个重点&#xff1a;过滤器和排序。大家有没有发现这个效果&#xff0c;有点像xx点评: 大家选店一般在口味/距离/价格能接受的前提下&#xff0c;选评分最高的那家店hh&#xff0c;这就是过滤器的效果。排序是说&#xff0c;对满足…

01-工具篇-windows与linux文件共享

一般来说绝大部分PC上装的系统均是windows&#xff0c;为了开发linux程序&#xff0c;会在PC上安装一个Vmware的虚拟机&#xff0c;在虚拟机上安装ubuntu18.04&#xff0c;由于windows上的代码查看软件、浏览器&#xff0c;通信软件更全&#xff0c;我们想只用ubuntu进行编译&a…

2.安装conda python库

centos 显卡驱动、cuda、cudnn安装参考地址&#xff1a; 1.centos7 安装显卡驱动、cuda、cudnn-CSDN博客 1.安装anaconda 步骤1&#xff1a;下载 下载地址&#xff1a; Index of / 下载版本 步骤2&#xff1a;运行 sh Anaconda3-2021.05-Linux-x86_64.sh 一路执行回车&…

单例模式:饿汉式

单例模式全局仅一个实例&#xff0c;用于获取公共的内容 头文件mglobalinfomgr.h class MGlobalInfoMgr {MGlobalInfoMgr();~MGlobalInfoMgr(); public:static MGlobalInfoMgr* GetInstance(); private:static MGlobalInfoMgr* _instance; }; 源文件mglobalinfomgr.cpp MGl…

API接口文档1688阿里巴巴获取跨境属性数据

API接口文档的作用和意义&#xff1a; 明确需要的接口服务 API分为很多种&#xff0c;最基础也是产品最需要的诸如短信API&#xff0c;地图API&#xff0c;语音API等&#xff0c;如果我们的产品涉及到此方面的功能&#xff0c;那就必须了解这方面的API以便于在需求设计阶段考…

阿里云服务器企业级独享和共享型有什么区别?

阿里云ECS云服务器共享型和企业级有什么区别&#xff1f;企业级就是独享型&#xff0c;共享型和企业级云的主要区别CPU调度模式&#xff0c;共享型是非绑定CPU调度模式&#xff0c;企业级是固定CPU调度模式&#xff0c;共享型云服务器在高负载时计算性能可能出现波动不稳定&…

5153. 删除

题目&#xff1a; 样例1&#xff1a; 输入 3454 输出 YES 344 样例2&#xff1a; 输入 10 输出 YES 0 样例3&#xff1a; 输入 111111 输出 NO 思路&#xff1a; 这道题就三个条件 关键条件是 能够被 8 整除。 而能被 8 整除的有一个重要的性质是 能够被 8 整除的重要…

CDS(一)

Core Data Services 核心数据服务 DDL 定义Query Language 查询DCL 控制&#xff08;权限相关&#xff09;model&#xff0c;从语义层获取数据open SQL 访问CDS viewCDS权限定义,集成旧的权限概念扩展native SQL扩展SQL建模和声明 ABAP ViewCDS View支持所有数据库是是支持查询…