感觉应该算是简单题
思路:
- 按照他提出的要求进行筛选即可
- 学习了 sort 函数中几个参数的用法, key=lambda c:(c[1], c[0]) 的形式可以令在排序中当c[1]相等时按照c[0]大小进行排序,reverse=True 实现降序排列
class Solution:
def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]:
ans = []
for rest in restaurants:
if veganFriendly == 1 and rest[2] != 1:
continue
if rest[3] > maxPrice or rest[4] > maxDistance:
continue
ans.append(rest)
ans.sort(key=lambda c:(c[1], c[0]), reverse=True)
return [i[0] for i in ans]