[LeetCode] Find Kth Largest XOR Coordinate Value

1738. Find Kth Largest XOR Coordinate Value

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int kthLargestValue(vector<vector<int>>& matrix, int k) {
vector<int> A;
int n = matrix.size(), m = matrix[0].size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(i) matrix[i][j] ^= matrix[i-1][j];
if(j) matrix[i][j] ^= matrix[i][j-1];
if(i and j) matrix[i][j] ^= matrix[i-1][j-1];
A.push_back(matrix[i][j]);
}
}
sort(rbegin(A), rend(A));
return A[k-1];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/30/PS/LeetCode/find-kth-largest-xor-coordinate-value/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.