Given the array restaurants where &nbsp;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)&nbsp;or false&nbsp;(meaning you can include any restaurant). In addition, you have the filters&nbsp;maxPrice and maxDistance&nbsp;which&nbsp;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.

&nbsp;
Example 1:

Input: 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
Output: [3,1,5] 
Explanation: 
The restaurants are:
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 restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest). 


Example 2:

Input: 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
Output: [4,3,2,1,5]
Explanation: The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.


Example 3:

Input: 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
Output: [4,5]


&nbsp;
Constraints:


	1 &lt;=&nbsp;restaurants.length &lt;= 10^4
	restaurants[i].length == 5
	1 &lt;=&nbsp;idi, ratingi, pricei, distancei &lt;= 10^5
	1 &lt;=&nbsp;maxPrice,&nbsp;maxDistance &lt;= 10^5
	veganFriendlyi and&nbsp;veganFriendly&nbsp;are&nbsp;0 or 1.
	All idi are distinct.


class Solution:
def f_fcn(self,restaurants, veganFriendly, maxPrice, maxDistance):
	f_lst = filter(lambda x: (veganFriendly == 1 and x[2] == 1 and x[3] <= maxPrice and x[4] <= maxDistance) or
				   (veganFriendly == 0 and x[3] <= maxPrice and x[4] <= maxDistance), restaurants)
	return f_lst

def h_fcn(self,lst):
	return([lst[0], lst[1]])

def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:

	res = map(self.h_fcn, self.f_fcn(restaurants, veganFriendly, maxPrice, maxDistance))


	return map(lambda x: x[0], sorted(res, key=lambda x: (-x[1], -x[0])))
class Restaurant {
    int id, rating;
    Restaurant(int id, int rating) {
        this.id = id;
        this.rating = rating;
    }
}

class RestaurantComparator implements Comparator<Restaurant> {
    @Override
    public int compare(Restaurant r1, Restaurant r2) {
        return r1.rating == r2.rating ? r2.id - r1.id : r2.rating - r1.rating;
    }
}

class Solution {
    public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
        PriorityQueue<Restaurant> heap = new PriorityQueue<>(new RestaurantComparator());
        if(veganFriendly == 1) {
            for(int[] restaurant: restaurants) {
                if(restaurant[2] == 1 && restaurant[3] <= maxPrice && restaurant[4] <= maxDistance) {
                    heap.offer(new Restaurant(restaurant[0], restaurant[1]));
                }
            }
        } else {
            for(int[] restaurant: restaurants) {
                if(restaurant[3] <= maxPrice && restaurant[4] <= maxDistance) {
                    heap.offer(new Restaurant(restaurant[0], restaurant[1]));
                }
            }
        }
        List<Integer> answer = new ArrayList<>();
        while(!heap.isEmpty()) {
            answer.add(heap.poll().id);
        }
        return answer;
    }
}
//comparator class for sorting restaurants by their rating 
class comp{
        public:
        bool operator ()(vector<int>a,vector<int>b){
		//same rating then sort by ids
                if(a[1]==b[1]) return a[0]>b[0];
                return a[1]>b[1];
        }
};
class Solution {
public:
        vector<int> filterRestaurants(vector<vector<int>>& restaurants, int veganFriendly, int maxPrice, int maxDistance)
		{ //sort restaurants by their rating
            sort(restaurants.begin(),restaurants.end(),comp());
            vector<int>ans;
            for(int i=0;i<restaurants.size();i++){
			//veganfriendly
                    if(veganFriendly)
					{
					// store ids who satisfy the constraints
                    if(restaurants[i][2]==veganFriendly and restaurants[i][3]<=maxPrice and  restaurants[i][4]<=maxDistance)
					        {
                            ans.push_back(restaurants[i][0]);
                            }
                    }
					// non vegan
                    else{
					// store ids who satisfy the constraints
                            if(restaurants[i][3]<=maxPrice and  restaurants[i][4]<=maxDistance)
							{
                            ans.push_back(restaurants[i][0]);
                            }
                    }
            }
         return ans;
    }
};
var filterRestaurants = function(restaurants, veganFriendly, maxPrice, maxDistance) {
    let result = []
    // veganFriendly filter
    if(veganFriendly === 1){
       restaurants = restaurants.filter(restaurant=> restaurant[2] === 1) 
    }
    
    //max price
    restaurants = restaurants.filter(restaurant=>restaurant[3]<=maxPrice)
    
    // max distance
    restaurants = restaurants.filter(restaurant=>restaurant[4]<=maxDistance)
    
    restaurants.sort((a,b)=>b[1]-a[1])
    
    let tempArr = []
    for(let i=0; i<restaurants.length;i++){
        if(restaurants[i+1] && restaurants[i][1]===restaurants[i+1][1] ){
            tempArr.push(restaurants[i][0])
            tempArr.push(restaurants[i+1][0])
            i++
        }
        else{
            if(tempArr.length>0){
                tempArr.sort((a,b)=>b-a)
                result =[...result,...tempArr]    
                tempArr=[]
            }            
            result.push(restaurants[i][0])
        }
            
    }
        
    return result
};
Filter Restaurants by Vegan-Friendly, Price and Distance