餐廳過濾器

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主:餵你腳下有坑

發佈了26 篇原創文章 · 獲贊 0 · 訪問量 614
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章