|
72 | 72 |
|
73 | 73 | <!-- solution:start -->
|
74 | 74 |
|
75 |
| -### Solution 1 |
| 75 | +### Solution 1: Offline Query + BFS + Priority Queue (Min Heap) |
| 76 | + |
| 77 | +According to the problem description, each query is independent, the order of the queries does not affect the result, and we are required to start from the top left corner each time, counting the number of cells that can be accessed and whose value is less than the current query value. |
| 78 | + |
| 79 | +Therefore, we can first sort the `queries` array, and then process each query in ascending order. |
| 80 | + |
| 81 | +We use a priority queue (min heap) to maintain the smallest cell value that we have currently accessed, and use an array or hash table `vis` to record whether the current cell has been visited. Initially, we add the data $(grid[0][0], 0, 0)$ of the top left cell as a tuple to the priority queue, and set `vis[0][0]` to `True`. |
| 82 | + |
| 83 | +For each query `queries[i]`, we judge whether the minimum value of the current priority queue is less than `queries[i]`. If it is, we pop the current minimum value, increment the counter `cnt`, and add the four cells above, below, left, and right of the current cell to the priority queue, noting to check whether they have been visited. Repeat the above operation until the minimum value of the current priority queue is greater than or equal to `queries[i]`, at which point `cnt` is the answer to the current query. |
| 84 | + |
| 85 | +The time complexity is $O(k \times \log k + m \times n \log(m \times n))$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the grid, and $k$ is the number of queries. We need to sort the `queries` array, which has a time complexity of $O(k \times \log k)$. Each cell in the matrix will be visited at most once, and the time complexity of each enqueue and dequeue operation is $O(\log(m \times n))$. Therefore, the total time complexity is $O(k \times \log k + m \times n \log(m \times n))$. |
76 | 86 |
|
77 | 87 | <!-- tabs:start -->
|
78 | 88 |
|
@@ -236,51 +246,4 @@ func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1];
|
236 | 246 |
|
237 | 247 | <!-- solution:end -->
|
238 | 248 |
|
239 |
| -<!-- solution:start --> |
240 |
| - |
241 |
| -### Solution 2 |
242 |
| - |
243 |
| -<!-- tabs:start --> |
244 |
| - |
245 |
| -#### Python3 |
246 |
| - |
247 |
| -```python |
248 |
| -class Solution: |
249 |
| - def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]: |
250 |
| - def find(x): |
251 |
| - if p[x] != x: |
252 |
| - p[x] = find(p[x]) |
253 |
| - return p[x] |
254 |
| - |
255 |
| - def union(a, b): |
256 |
| - pa, pb = find(a), find(b) |
257 |
| - if pa == pb: |
258 |
| - return |
259 |
| - p[pa] = pb |
260 |
| - size[pb] += size[pa] |
261 |
| - |
262 |
| - m, n = len(grid), len(grid[0]) |
263 |
| - arr = sorted((grid[i][j], i, j) for i in range(m) for j in range(n)) |
264 |
| - k = len(queries) |
265 |
| - ans = [0] * k |
266 |
| - p = list(range(m * n)) |
267 |
| - size = [1] * len(p) |
268 |
| - j = 0 |
269 |
| - for i, v in sorted(enumerate(queries), key=lambda x: x[1]): |
270 |
| - while j < len(arr) and arr[j][0] < v: |
271 |
| - _, a, b = arr[j] |
272 |
| - for x, y in pairwise((-1, 0, 1, 0, -1)): |
273 |
| - c, d = a + x, b + y |
274 |
| - if 0 <= c < m and 0 <= d < n and grid[c][d] < v: |
275 |
| - union(a * n + b, c * n + d) |
276 |
| - j += 1 |
277 |
| - if grid[0][0] < v: |
278 |
| - ans[i] = size[find(0)] |
279 |
| - return ans |
280 |
| -``` |
281 |
| - |
282 |
| -<!-- tabs:end --> |
283 |
| - |
284 |
| -<!-- solution:end --> |
285 |
| - |
286 | 249 | <!-- problem:end -->
|
0 commit comments