- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array of restaurants where restaurants[i] have [idi, ratingi, vegan friendly, pricei, distancei]. We have to filter the restaurants using three filters.

The vegan-friendly filter will be either true (meaning we should only include restaurants with vegan-friendly set to true) or false (meaning we can include any restaurant).

The maxPrice filter and max distance filter which are the maximum value for price and distance of restaurants we should consider respectively.

We have to find the array of restaurant IDs after filtering, ordered by rating from highest to lowest. For restaurants with the same rating, order them by id in decreasing order. For simplicity vegan friendly and vegan-friendly take value 1 when it is true, and 0 when it is false.

So if the input is like 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, then the output will be [3,1,5], the explanation is like below −

Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]

Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]

Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]

Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]

Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1]

After filter the restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating in decreasing order).

To solve this, we will follow these steps −

Define a matrix called temp, n := size of restaurants array

for i in range 0 to n – 1

if vf = 0 or r[i, 2] = vf and r[i, 3] <= mp and r[i, 4] <= md, then

insert [r[i, 0], r[i, 1]] into temp

Sort the restaurants in decreasing order based on the ratings

make an array called ret

for i in range 0 to size of temp

insert temp[i, 0] into ret

return ret

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: static bool cmp(vector <int> a, vector <int> b){ if(b[1] != a[1])return a[1] > b[1]; return a[0] > b[0]; } vector<int>filterRestaurants(vector<vector<int>>& r, int vf, int mp, int md) { vector < vector <int> > temp; int n = r.size(); for(int i = 0; i < n; i++){ if((vf == 0 || r[i][2] == vf) && r[i][3] <= mp && r[i][4] <= md){ temp.push_back({r[i][0], r[i][1]}); } } sort(temp.begin(), temp.end(), cmp); vector <int> ret; for(int i = 0; i < temp.size(); i++)ret.push_back(temp[i][0]); return ret; } }; main(){ vector<vector<int>> v = {{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}}; Solution ob; print_vector(ob.filterRestaurants(v, 1, 50, 10)); }

[[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]] 1 50 10

[3,1,5]

- Related Questions & Answers
- Compare bid price and offer price
- Sorting an array by price in JavaScript
- Find cost price from given selling price and profit or loss percentage in C++
- C++ Program to find winner and final price in a second price auction event
- What are the User friendly interfaces provided by DBMS?
- Edit Distance in C++
- Differentiate between price, value and cost
- Filter array with filter() and includes() in JavaScript
- Filter by several array elements in MongoDB?
- Filter query by current date in MySQL
- Find Selling Price from given Profit Percentage and Cost in C++
- JavaScript filter array by multiple strings?
- Python - Filter Pandas DataFrame by Time
- Count paths with distance equal to Manhattan distance in C++
- Total Hamming Distance in C++

Advertisements