K Highest Ranked Items Within a Price Range LeetCode solution in Python
1# Runtime: 3258 ms (Top 98.66%) | Memory: 66 MB (Top 18.12%)2class Solution:3def highestRankedKItems(self, G, pricing, start, k):4m, n = len(G), len(G[0])5row, col = start6node = (0, G[row][col], row, col)7visited = set()8visited.add((row, col))9d = deque([node])10ans = []1112while d:13dist, cost, row, col = d.popleft()14if pricing[0] <= cost <= pricing[1]:15ans += [(dist, cost, row, col)]1617for x, y in (row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1):18if (190 <= x <= m - 120and 0 <= y <= n - 121and (x, y) not in visited22and G[x][y] != 023):24d.append((dist + 1, G[x][y], x, y))25visited.add((x, y))2627ans = sorted(ans)2829return [[x, y] for _, _, x, y in ans[:k]]