avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1333.Restaurant filter

  2024-01-01        
字数统计: 231字   |   阅读时长: 1min

topic:

screenshot2023-09-27 afternoon3.45.32.png

1333.Restaurant filter.md

Thought:

This is the beginningpopThought,But time complexity is too high,400ms,It is later changed to a list derivative。
I think of the senior said,pop()The running time is okay,But once the index is added inside,It will be particularly slow。
sorted and lambda Usage:
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。

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。

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
11
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]
  • Python
  • answer
  • Array
  • Sort

扫一扫,分享到微信

微信分享二维码
1222.Can attack the queen of the king
1253.Reconstruct 2 Line binary matrix
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号