Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 311f339

Browse files
committed
2020-01-26
1 parent 4df208c commit 311f339

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def filterRestaurants(self, restaurants, veganFriendly, maxPrice, maxDistance):
3+
"""
4+
:type restaurants: List[List[int]]
5+
:type veganFriendly: int
6+
:type maxPrice: int
7+
:type maxDistance: int
8+
:rtype: List[int]
9+
"""
10+
if not restaurants:
11+
return []
12+
13+
res = []
14+
for i, rating, vF, mP, mD in restaurants:
15+
if not veganFriendly or (vF and veganFriendly):
16+
if mP <= maxPrice and mD <= maxDistance:
17+
res.append([i, rating])
18+
19+
return [i for i, rating in sorted(res, key = lambda x:(x[1], x[0]), reverse = True)]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def findTheCity(self, n, edges, distanceThreshold):
3+
"""
4+
:type n: int
5+
:type edges: List[List[int]]
6+
:type distanceThreshold: int
7+
:rtype: int
8+
"""
9+
distance = [[float("inf") for j in range(n)] for i in range(n)]
10+
11+
for start, end, w in edges:
12+
distance[start][end] = w
13+
distance[end][start] = w
14+
for i in range(n):
15+
distance[i][i] = 0
16+
for i in range(n):
17+
for j in range(n):
18+
for k in range(n):
19+
distance[j][k] = min(distance[j][k], distance[j][i] + distance[i][k])
20+
21+
min_cnt = 101
22+
res = None
23+
for i in range(n):
24+
cnt = 0
25+
for j in range(n):
26+
if distance[i][j] <= distanceThreshold:
27+
cnt += 1
28+
29+
if cnt <= min_cnt:
30+
res = i
31+
min_cnt = cnt
32+
return res
33+

0 commit comments

Comments
 (0)