Filter Restaurants by Vegan-Friendly, Price and Distance in C++


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

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#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));
}

Input

[[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

Output

[3,1,5]

Updated on: 29-Apr-2020

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements