Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:
- m, n = len(grid), len(grid[0])
- dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
- queue = deque([start])
- res = []
- if pricing[0]<=grid[start[0]][start[1]]<=pricing[1]:
- res.append(start)
- vis = set([(start[0], start[1])])
- while queue:
- sz = len(queue)
- tmp = []
- for _ in range(sz):
- a, b = queue.pop()
- for x, y in dirs:
- if 0<=a+x<m and 0<=b+y<n and (a+x, b+y) not in vis and grid[a+x][b+y] > 0:
- queue.appendleft([a+x, b+y])
- vis.add((a+x, b+y))
- if pricing[0]<=grid[a+x][b+y]<=pricing[1]:
- tmp.append([a+x, b+y])
- tmp.sort(key=lambda x: (grid[x[0]][x[1]], x[0], x[1]))
- res += tmp
- if len(res) >= k:
- break
- return res[:k]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement