Skip to content

Commit 6f96862

Browse files
committed
Sync LeetCode submission Runtime - 1023 ms (20.14%), Memory - 32.7 MB (63.09%)
1 parent b60ccce commit 6f96862

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<p>You are given an <code>m x n</code> integer matrix <code>grid</code> and an array <code>queries</code> of size <code>k</code>.</p>
2+
3+
<p>Find an array <code>answer</code> of size <code>k</code> such that for each integer <code>queries[i]</code> you start in the <strong>top left</strong> cell of the matrix and repeat the following process:</p>
4+
5+
<ul>
6+
<li>If <code>queries[i]</code> is <strong>strictly</strong> greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any <strong>adjacent</strong> cell in all <code>4</code> directions: up, down, left, and right.</li>
7+
<li>Otherwise, you do not get any points, and you end this process.</li>
8+
</ul>
9+
10+
<p>After the process, <code>answer[i]</code> is the <strong>maximum</strong> number of points you can get. <strong>Note</strong> that for each query you are allowed to visit the same cell <strong>multiple</strong> times.</p>
11+
12+
<p>Return <em>the resulting array</em> <code>answer</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2025/03/15/image1.png" style="width: 571px; height: 152px;" />
17+
<pre>
18+
<strong>Input:</strong> grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]
19+
<strong>Output:</strong> [5,8,1]
20+
<strong>Explanation:</strong> The diagrams above show which cells we visit to get points for each query.</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/20/yetgriddrawio-2.png" />
24+
<pre>
25+
<strong>Input:</strong> grid = [[5,2,1],[1,1,2]], queries = [3]
26+
<strong>Output:</strong> [0]
27+
<strong>Explanation:</strong> We can not get any points because the value of the top left cell is already greater than or equal to 3.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>m == grid.length</code></li>
35+
<li><code>n == grid[i].length</code></li>
36+
<li><code>2 &lt;= m, n &lt;= 1000</code></li>
37+
<li><code>4 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
38+
<li><code>k == queries.length</code></li>
39+
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
40+
<li><code>1 &lt;= grid[i][j], queries[i] &lt;= 10<sup>6</sup></code></li>
41+
</ul>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Approach 2: Sorting Queries + Min-Heap Expansion
2+
3+
# n, m = rows, cols in grid
4+
# k = len(queries)
5+
# Time: O(k log k + n * m log (n * m))
6+
# Space: O(n * m + k)
7+
8+
from queue import PriorityQueue
9+
10+
class Solution:
11+
def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:
12+
row_count, col_count = len(grid), len(grid[0])
13+
result = [0] * len(queries)
14+
DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
15+
16+
sorted_queries = sorted([(val, idx) for idx, val in enumerate(queries)])
17+
18+
min_heap = PriorityQueue()
19+
visited = [[False] * col_count for _ in range(row_count)]
20+
21+
total_points = 0
22+
23+
min_heap.put((grid[0][0], 0, 0))
24+
visited[0][0] = True
25+
26+
for query_value, query_index in sorted_queries:
27+
while not min_heap.empty() and min_heap.queue[0][0] < query_value:
28+
cell_value, curr_row, curr_col = min_heap.get()
29+
30+
total_points += 1
31+
32+
for row_offset, col_offset in DIRECTIONS:
33+
new_row, new_col = curr_row + row_offset, curr_col + col_offset
34+
35+
if (
36+
new_row >= 0
37+
and new_col >= 0
38+
and new_row < row_count
39+
and new_col < col_count
40+
and not visited[new_row][new_col]
41+
):
42+
min_heap.put((grid[new_row][new_col], new_row, new_col))
43+
visited[new_row][new_col] = True
44+
45+
result[query_index] = total_points
46+
47+
return result

0 commit comments

Comments
 (0)