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主:喂你脚下有坑