Skip to content

Commit f7c7823

Browse files
committed
Mar 28
1 parent e61d86c commit f7c7823

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from heapq import heappop, heappush
2+
3+
4+
class Solution:
5+
DIRECTIONS = ((-1, 0), (1, 0), (0, -1), (0, 1))
6+
7+
def maxPoints(self, grid: list[list[int]], queries: list[int]) -> list[int]:
8+
queries = [(query, idx) for idx, query in enumerate(queries)]
9+
queries.sort()
10+
rows, cols = len(grid), len(grid[0])
11+
result = [0] * len(queries)
12+
visited = [[False] * cols for _ in range(rows)]
13+
heap = [(grid[0][0], 0, 0)]
14+
visited[0][0] = True
15+
points = 0
16+
for query, idx in queries:
17+
while heap and heap[0][0] < query:
18+
_, x, y = heappop(heap)
19+
points += 1
20+
for dx, dy in self.DIRECTIONS:
21+
nx, ny = x + dx, y + dy
22+
if 0 <= nx < rows and 0 <= ny < cols and not visited[nx][ny]:
23+
heappush(heap, (grid[nx][ny], nx, ny))
24+
visited[nx][ny] = True
25+
result[idx] = points
26+
return result
27+
28+
29+
def main():
30+
grid = [[1, 2, 3],
31+
[2, 5, 7],
32+
[3, 5, 1]]
33+
queries = [5, 6, 2]
34+
assert Solution().maxPoints(grid, queries) == [5, 8, 1]
35+
36+
grid = [[5, 2, 1],
37+
[1, 1, 2]]
38+
queries = [3]
39+
assert Solution().maxPoints(grid, queries) == [0]
40+
41+
42+
if __name__ == '__main__':
43+
main()

2025-03-March-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
| March 25 | [3394. Check if Grid can be Cut into Sections](https://leetcode.com/problems/check-if-grid-can-be-cut-into-sections/) | Medium | Solved |
3232
| March 26 | [2033. Minimum Operations to Make a Uni-Value Grid](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/) | Medium | Solved |
3333
| March 27 | [2780. Minimum Index of a Valid Split](https://leetcode.com/problems/minimum-index-of-a-valid-split/) | Medium | Unsolved |
34-
| March 28 | []() | | |
34+
| March 28 | [2503. Maximum Number of Points From Grid Queries](https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/) | Hard | Unsolved |
3535
| March 29 | []() | | |
3636
| March 30 | []() | | |
3737
| March 31 | []() | | |
@@ -42,4 +42,4 @@
4242
| --- | --- | --- | --- |
4343
| Easy | 6 | 6 | 0 |
4444
| Medium | 20 | 9 | 11 |
45-
| Hard | 1 | 0 | 1 |
45+
| Hard | 2 | 0 | 2 |

0 commit comments

Comments
 (0)