Filter Restaurants by Vegan-Friendly, Price and Distance
给一堆餐馆, 求找到符合条件的. 这个就把不符合的先筛去, 然后放到pq里按照rating排序.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Solution { public List<Integer> filterRestaurants(int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) { PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> ( b[1] == a[1] ? b[0] - a[0] : b[1] - a[1] )); for(int[] r : restaurants) { if(veganFriendly == 1 && r[2] != veganFriendly) continue; else if(r[3] > maxPrice) continue; else if(r[4] > maxDistance) continue; else q.add(new int[]{r[0], r[1]}); } List<Integer> res = new ArrayList<>(); if(q.isEmpty()) return res; else{ while(!q.isEmpty()){ res.add(q.poll()[0]); } } return res; } } |