餐厅过滤器

1333.餐厅过滤器
一、
用户通过次数 412
用户尝试次数 501
通过次数 418
提交次数 1317
题目难度 Medium

给你一个餐馆信息数组 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 各不相同。

英文描述
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.

二、向量的声明及初始化
vector 型变量的声明以及初始化的形式也有许多, 常用的有以下几种形式:
vector a ; //声明一个int型向量a
vector a(10) ; //声明一个初始大小为10的向量
vector a(10, 1) ; //声明一个初始大小为10且初始值都为1的向量
vector b(a) ; //声明并用向量a初始化向量b
vector b(a.begin(), a.begin()+3) ; //将a向量中从第0个到第2个(共3个)作为向量b的初始值
**#include
#include

using namespace std;

int main()
{
vector<vector> A;
vector B;

B.push_back(0);
B.push_back(1);
B.push_back(2);
B.push_back(3);
A.push_back(B);

//注意需要清空B
B.clear();
B.push_back(4);
B.push_back(5);
B.push_back(6);
B.push_back(7);
A.push_back(B);

cout << "============第一种索引方式============" << endl;
for (int i = 0; i < 2; i++)
{
    vector<int> & p = A[i];
    for (int j = 0; j < p.size(); j++)
    {
        cout << p[j] << " ";
    }
    cout << endl;
}

cout << "============第二种索引方式============" << endl;
for (int i = 0; i < A.size(); i++)
{
    for (int j = 0; j < A[0].size();j++)
        cout << A[i][j] << " ";

    cout << endl;
}

return  0;

}**
auto迭代:
对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此C++11中引入了基于范围的for循环。for循环后的括号由冒号“ :”分为两部分:第一部分是范围内用于迭代的变量,第二部分则表示被迭代的范围

vector中的sort函数(第三个参数可以自定义排序函数):
1.包含头文件 #include,然后using namespace std;
2.假如你定义的vector变量为vector num,则如下:
sort(num.begin(), num.end(), sortFun);
然后如果是基本类型假如是int,第三个参数可以使用系统自带的less()或者greater(),假如是自定义类型话或者复杂类型就需自己定义比较规则函数sortFun,以下以opencv中的Point2d类型举例:

#include
#include
#include
#include<opencv2\opencv.hpp>
using namespace std;
using namespace cv;
vectorcv::Point2d po;

自定义排序函数
bool sortFun(const cv::Point2d &p1, const cv::Point2d &p2)
{
return p1.x < p2.x;//升序排列
}

int main()
{
Point2d p1(2, 4), p2(4, 3), p3(1, 7), p4(0,4);
po.push_back(p1);
po.push_back(p2);
po.push_back(p3);
po.push_back(p4);
cout << "排序前: ";
for (auto elem : po)
cout << elem << " ";

sort(po.begin(), po.end(), sortFun);
cout << endl << "排序后: " ;
for (auto elem : po)
	cout << elem << " ";

cout << endl;
system("pause");
return 0;

}

代码:

vector<vector<int>> ans;
inline bool cmp(const vector<int>&a,const vector<int>&b)
{
    if(a[1]!=b[1])return a[1]>b[1];
    return a[0]>b[0];
}
class Solution {
public:
    vector<int> filterRestaurants(vector<vector<int>>& restaurants, int veganFriendly, int maxPrice, int maxDistance) {
        ans.clear();
        for(auto r:restaurants)
        {
            int id=r[0],rating=r[1];
            int vegan=r[2],price=r[3],dist=r[4];
            if(veganFriendly==1&&vegan==0)continue;
            if(price>maxPrice)continue;
            if(dist>maxDistance)continue;
            ans.push_back(r);
        }
        sort(ans.begin(),ans.end(),cmp);
       vector<int>ret;
        for(auto r:ans)ret.push_back(r[0]);
        return ret;
    }
};

以上解决代码来自up主:喂你脚下有坑

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 、可私信6博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值