内卷地狱

1333. Filter Restaurants by Vegan-Friendly, Price, and Distance

Edit Me

Problem

1333. Filter Restaurants by Vegan-Friendly, Price, and Distance

Approach

My initial approach used pop(), but the time complexity was too high (~400ms). I switched to a list comprehension.

A senior once told me: pop() runtime is fine, but when you combine it with index operations inside, it becomes very slow.

sorted and lambda usage:

lambda is Python's anonymous function syntax. Here, lambda x: (x[1], x[0]) defines a function that takes an element x (a sublist of restaurants) and returns a tuple (x[1], x[0]).

This means sorting is first based on x[1] (rating), then by x[0] (id) if x[1] values are equal.

while ind < len(restaurants):
     i = restaurants[ind]
     if veganFriendly == 1 and i[2] == 0:
         restaurants.pop(ind)
     elif maxPrice < i[3]:
         restaurants.pop(ind)
     elif maxDistance < i[4]:
         restaurants.pop(ind)
     else:
         ind += 1

Code

class Solution:
    def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> \
            List[int]:
        restaurants = [
            i for i in restaurants
            if (veganFriendly == 0 or i[2] == veganFriendly)
               and i[3] <= maxPrice
               and i[4] <= maxDistance
        ]
        restaurants = sorted(restaurants, key=lambda x: (x[1], x[0]), reverse=True)
        return [i[0] for i in restaurants]

贡献者


这篇文章有帮助吗?

最近更新

Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0CCBYNCSA