aboutsummaryrefslogtreecommitdiff
path: root/filter_restaurants_vpd.py
blob: b7da9453c812289dcc6ecb39842ea208df86fd71 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 1333. Filter Restaurants by Vegan-Friendly, Price and Distance

"""
given the array 'restaurants' where 'restaurants[i] = [id_i, rating_i,
veganFriendly_i, price_i, distance_i]'. you have to filter the restaurants
using three filters. the 'veganFriendly' filter will either be true or false.
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 id's after filtering, ordered by
rating from highest to lowest. for restaurants with the same rating, order
them by id from highest to lowest.
"""


class Solution(object):
    def filterRestaurants(self, restaurants, veganFriendly, maxPrice, maxDistance):
        """
        :type restaurants: List[List[int]]
        :type veganFriendly: int
        :type maxPrice: int
        :type maxDistance: int
        :rtype: List[int]
        """
        restaurants.sort(key=lambda x: (-x[1], -x[0]))
        return [
            i
            for i, _, v, p, d in restaurants
            if v >= veganFriendly and p <= maxPrice and d <= maxDistance
        ]


if __name__ == "__main__":
    obj = Solution()
    print(
        obj.filterRestaurants(
            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,
        )
    )
    print(
        obj.filterRestaurants(
            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,
        )
    )
    print(
        obj.filterRestaurants(
            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,
        )
    )