LeetCode

Editorial: LeetCode 1738 Find Kth Largest Xor Coordinate Value

Editorial: LeetCode 1738 Find Kth Largest Xor Coordinate Value

https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/ from Google

Thinking process:

Just follow the problem statement. So we first modify the matrix in place so that the matrix[i][j] is the XOR of all values before matrix[i][j] in the same row.

After this, we just need to xor matrix[i][j] with matrix[i-1][j] to get the real value we want, and sort them to get the answer.

Here is the code.

 1class Solution {
 2public:
 3    int kthLargestValue(vector<vector<int>>& matrix, int k) {
 4        vector<vector<int>> m;
 5        int R = matrix.size();
 6        int C = matrix[0].size();
 7        for (int i=0; i<R; i++) {
 8            for (int j=0; j<C; j++) {
 9                if (j == 0) {
10                } else {
11                    matrix[i][j] = matrix[i][j]^matrix[i][j-1];
12                }
13            }
14        }
15        m.resize(R, vector<int>(C));
16        vector<int> v;
17        for (int i=0; i<R; i++) {
18            for (int j=0; j<C; j++) {
19                if (i == 0) {
20                    m[i][j] = matrix[i][j];
21                } else {
22                    m[i][j] = matrix[i][j]^m[i-1][j];
23                }
24                v.push_back(m[i][j]);
25            }
26        }
27        sort(v.rbegin(), v.rend());
28        return v[k-1];
29    }
30};
comments powered by Disqus