forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
35 lines (35 loc) · 1.2 KB
/
Solution.java
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
class Solution {
public int[] maxPoints(int[][] grid, int[] queries) {
int k = queries.length;
int[][] qs = new int[k][2];
for (int i = 0; i < k; ++i) {
qs[i] = new int[] {queries[i], i};
}
Arrays.sort(qs, (a, b) -> a[0] - b[0]);
int[] ans = new int[k];
int m = grid.length, n = grid[0].length;
boolean[][] vis = new boolean[m][n];
vis[0][0] = true;
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
q.offer(new int[] {grid[0][0], 0, 0});
int[] dirs = new int[] {-1, 0, 1, 0, -1};
int cnt = 0;
for (var e : qs) {
int v = e[0];
k = e[1];
while (!q.isEmpty() && q.peek()[0] < v) {
var p = q.poll();
++cnt;
for (int h = 0; h < 4; ++h) {
int x = p[1] + dirs[h], y = p[2] + dirs[h + 1];
if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y]) {
vis[x][y] = true;
q.offer(new int[] {grid[x][y], x, y});
}
}
}
ans[k] = cnt;
}
return ans;
}
}