Skip to content

Commit

Permalink
Create find-kth-largest-xor-coordinate-value.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jan 25, 2021
1 parent bb1c5c0 commit 681bfc0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions C++/find-kth-largest-xor-coordinate-value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time: O(m * n) on average
// Space: O(m * n)

class Solution {
public:
int kthLargestValue(vector<vector<int>>& matrix, int k) {
vector<int> vals;
for (int r = 0; r < size(matrix); ++r) {
int curr = 0;
for (int c = 0; c < size(matrix[0]); ++c) {
curr = curr ^ matrix[r][c];
if (r == 0) {
matrix[r][c] = curr;
} else {
matrix[r][c] = curr ^ matrix[r - 1][c];
}
vals.emplace_back(matrix[r][c]);
}
}
nth_element(begin(vals), begin(vals) + k - 1, end(vals), greater<int>());
return vals[k - 1];
}
};

0 comments on commit 681bfc0

Please sign in to comment.