1738. Find Kth Largest XOR Coordinate Value

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
class Solution {
public int kthLargestValue(int[][] matrix, int k) {
int m = matrix.length, n = matrix[0].length;

Queue<Integer> minHeap = new PriorityQueue<>();

int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = matrix[i][j];
if (i - 1 >= 0) {
dp[i][j] ^= dp[i - 1][j];
}
if (j - 1 >= 0) {
dp[i][j] ^= dp[i][j - 1];
}
if (i - 1 >= 0 && j - 1 >= 0) {
dp[i][j] ^= dp[i - 1][j - 1];
}

if (minHeap.size() < k) {
minHeap.offer(dp[i][j]);
} else if (dp[i][j] > minHeap.peek()) {
minHeap.offer(dp[i][j]);
minHeap.poll();
}
}
}

return minHeap.peek();
}
}

References

1738. Find Kth Largest XOR Coordinate Value