这道题目的含义就是先计算出所有二维前缀和,然后找出第k大的,排序,或者线性选择都可以
class Solution {
public:
int kthLargestValue(vector<vector<int>>& matrix, int k) {
int n = matrix.size(), m = matrix[0].size();
vector<vector<int>> s(n+1, vector<int>(m + 1));
vector<int> results;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
s[i][j] = s[i - 1][j - 1] ^ s[i - 1][j] ^ s[i][j - 1] ^ matrix[i - 1][j - 1];
results.push_back(s[i][j]);
}
}
nth_element(results.begin(), results.begin() + k - 1, results.end(), greater<int>());
return results[k - 1];
}
};