Skip to content

Commit 24b863f

Browse files
committed
chore: add daily leetcode post [1333]餐厅过滤器_translated
1 parent 4ffd6ff commit 24b863f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: 1333.Restaurant filter
3+
date: '2024.01.01 0:00'
4+
tags:
5+
- Python
6+
- answer
7+
- Sort
8+
- Array
9+
abbrlink: 7f1331bc
10+
---
11+
# topic:
12+
13+
14+
[1333.Restaurant filter.md](https://leetcode-cn.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)
15+
16+
# Thought:
17+
This is the beginningpopThought,But time complexity is too high,400ms,It is later changed to a list derivative。
18+
I think of the senior said,pop()The running time is okay,But once the index is added inside,It will be particularly slow。
19+
sorted and lambda Usage:
20+
lambdayesPythonAnonymous function in。it's here,lambda x: (x[1], x[0])Definitions a acceptance of an elementx(in this case,xyesrestaurantsA list in the list)And return a tuple(x[1], x[0])The function。
21+
22+
this means,Sort首先基于每个子列表的第二个元素x[1],Then based on this basisx[0]。in other words,It first followsx[1]进行Sort,ifx[1]same,According tox[0]进行Sort。
23+
```python
24+
while ind < len(restaurants):
25+
i = restaurants[ind]
26+
if veganFriendly == 1 and i[2] == 0:
27+
restaurants.pop(ind)
28+
elif maxPrice < i[3]:
29+
restaurants.pop(ind)
30+
elif maxDistance < i[4]:
31+
restaurants.pop(ind)
32+
else:
33+
ind += 1
34+
```
35+
36+
# Code:
37+
```python
38+
class Solution:
39+
def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> \
40+
List[int]:
41+
restaurants = [
42+
i for i in restaurants
43+
if (veganFriendly == 0 or i[2] == veganFriendly)
44+
and i[3] <= maxPrice
45+
and i[4] <= maxDistance
46+
]
47+
restaurants = sorted(restaurants, key=lambda x: (x[1], x[0]), reverse=True)
48+
return [i[0] for i in restaurants]
49+
```

0 commit comments

Comments
 (0)