-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2503-maximum-number-of-points-from-grid-queries.py
More file actions
52 lines (41 loc) · 1.58 KB
/
2503-maximum-number-of-points-from-grid-queries.py
File metadata and controls
52 lines (41 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# time complexity: O(klogk + n*mlog(n*m))
# space complexity: O(n*m + k)
from typing import List
from queue import PriorityQueue
class Solution:
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
ROW, COL = len(grid), len(grid[0])
result = [0] * len(queries)
DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
queries = sorted([(val, idx) for idx, val in enumerate(queries)])
minHeap = PriorityQueue()
visited = [[False] * COL for _ in range(ROW)]
totalPoints = 0
minHeap.put((grid[0][0], 0, 0))
visited[0][0] = True
for val, idx in queries:
while not minHeap.empty() and minHeap.queue[0][0] < val:
_, currR, currC = minHeap.get()
totalPoints += 1
for dR, dC in DIRECTIONS:
nextR, nextC = (
currR + dR,
currC + dC,
)
if (
nextR >= 0
and nextC >= 0
and nextR < ROW
and nextC < COL
and not visited[nextR][nextC]
):
minHeap.put((grid[nextR][nextC], nextR, nextC))
visited[nextR][nextC] = True
result[idx] = totalPoints
return result
grid = [[1, 2, 3], [2, 5, 7], [3, 5, 1]]
queries = [5, 6, 2]
print(Solution().maxPoints(grid, queries))
grid = [[5, 2, 1], [1, 1, 2]]
queries = [3]
print(Solution().maxPoints(grid, queries))