Filter Restaurants by Vegan-Friendly, Price and Distance — LeetCode 1333 Python Solution
MediumArraySorting
- Problem
- #1333
- Pattern
- Sorting
- Reading time
- 3 min
- Source
- leetcode.com
The problem
Given the array restaurants where restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei]. You have to filter the restaurants using three filters.
Example
- 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:
Python solution
Python
class Solution:
def filterRestaurants(
self,
restaurants: List[List[int]],
veganFriendly: int,
maxPrice: int,
maxDistance: int,
) -> List[int]:
restaurants.sort(key=lambda x: (-x[1], -x[0]))
ans = []
for idx, _, vegan, price, dist in restaurants:
if vegan >= veganFriendly and price <= maxPrice and dist <= maxDistance:
ans.append(idx)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) |
| Space | O(1) to O(n) auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1333. Filter Restaurants by Vegan-Friendly, Price and Distance is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1333. Filter Restaurants by Vegan-Friendly, Price and Distance?
- LeetCode 1333. Filter Restaurants by Vegan-Friendly, Price and Distance is rated Medium on LeetCode.
- What topics does LeetCode 1333. Filter Restaurants by Vegan-Friendly, Price and Distance cover?
- LeetCode 1333. Filter Restaurants by Vegan-Friendly, Price and Distance is tagged Array and Sorting on LeetCode.