Skip to content

Commit 681bfc0

Browse files
authored
Create find-kth-largest-xor-coordinate-value.cpp
1 parent bb1c5c0 commit 681bfc0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(m * n) on average
2+
// Space: O(m * n)
3+
4+
class Solution {
5+
public:
6+
int kthLargestValue(vector<vector<int>>& matrix, int k) {
7+
vector<int> vals;
8+
for (int r = 0; r < size(matrix); ++r) {
9+
int curr = 0;
10+
for (int c = 0; c < size(matrix[0]); ++c) {
11+
curr = curr ^ matrix[r][c];
12+
if (r == 0) {
13+
matrix[r][c] = curr;
14+
} else {
15+
matrix[r][c] = curr ^ matrix[r - 1][c];
16+
}
17+
vals.emplace_back(matrix[r][c]);
18+
}
19+
}
20+
nth_element(begin(vals), begin(vals) + k - 1, end(vals), greater<int>());
21+
return vals[k - 1];
22+
}
23+
};

0 commit comments

Comments
 (0)