You are given a 2D matrix
of size m x n
, consisting of non-negative integers. You are also given an integer k
.
The value of coordinate (a, b)
of the matrix is the XOR of all matrix[i][j]
where 0 <= i <= a < m
and 0 <= j <= b < n
(0-indexed).
Find the kth
largest value (1-indexed) of all the coordinates of matrix
.
Example 1:
Input: matrix = [[5,2],[1,6]], k = 1 Output: 7 Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
Example 2:
Input: matrix = [[5,2],[1,6]], k = 2 Output: 5 Explanation: The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.
Example 3:
Input: matrix = [[5,2],[1,6]], k = 3 Output: 4 Explanation: The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.
Example 4:
Input: matrix = [[5,2],[1,6]], k = 4 Output: 0 Explanation: The value of coordinate (1,1) is 5 XOR 2 XOR 1 XOR 6 = 0, which is the 4th largest value.
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
0 <= matrix[i][j] <= 106
1 <= k <= m * n
Related Topics:
Array
// OJ: https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/
// Author: github.com/lzl124631x
// Time: O(MNlog(MN))
// Space: O(MN)
class Solution {
public:
int kthLargestValue(vector<vector<int>>& A, int k) {
vector<int> v;
int M = A.size(), N = A[0].size();
for (int i = 0; i < M; ++i) {
int val = 0;
for (int j = 0; j < N; ++j) {
val ^= A[i][j];
A[i][j] = val;
if (i - 1 >= 0) A[i][j] ^= A[i - 1][j];
v.push_back(A[i][j]);
}
}
sort(begin(v), end(v), greater<>());
return v[k - 1];
}
};
Or use Heap
// OJ: https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/
// Author: github.com/lzl124631x
// Time: O(MNlog(K))
// Space: O(K)
class Solution {
public:
int kthLargestValue(vector<vector<int>>& A, int k) {
priority_queue<int, vector<int>, greater<>> pq;
int M= A.size(), N = A[0].size();
for (int i = 0; i < M; ++i) {
int val = 0;
for (int j = 0; j < N; ++j) {
val ^= A[i][j];
A[i][j] = val;
if (i - 1 >= 0) A[i][j] ^= A[i - 1][j];
if (pq.size() < k || A[i][j] > pq.top()) pq.push(A[i][j]);
if (pq.size() > k) pq.pop();
}
}
return pq.top();
}
};