|
| 1 | +// Source : https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-31 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an |
| 8 | + * integer k. |
| 9 | + * |
| 10 | + * The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m |
| 11 | + * and 0 <= j <= b < n (0-indexed). |
| 12 | + * |
| 13 | + * Find the k^th largest value (1-indexed) of all the coordinates of matrix. |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * |
| 17 | + * Input: matrix = [[5,2],[1,6]], k = 1 |
| 18 | + * Output: 7 |
| 19 | + * Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value. |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * |
| 23 | + * Input: matrix = [[5,2],[1,6]], k = 2 |
| 24 | + * Output: 5 |
| 25 | + * Explanation: The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value. |
| 26 | + * |
| 27 | + * Example 3: |
| 28 | + * |
| 29 | + * Input: matrix = [[5,2],[1,6]], k = 3 |
| 30 | + * Output: 4 |
| 31 | + * Explanation: The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value. |
| 32 | + * |
| 33 | + * Example 4: |
| 34 | + * |
| 35 | + * Input: matrix = [[5,2],[1,6]], k = 4 |
| 36 | + * Output: 0 |
| 37 | + * Explanation: The value of coordinate (1,1) is 5 XOR 2 XOR 1 XOR 6 = 0, which is the 4th largest |
| 38 | + * value. |
| 39 | + * |
| 40 | + * Constraints: |
| 41 | + * |
| 42 | + * m == matrix.length |
| 43 | + * n == matrix[i].length |
| 44 | + * 1 <= m, n <= 1000 |
| 45 | + * 0 <= matrix[i][j] <= 10^6 |
| 46 | + * 1 <= k <= m * n |
| 47 | + ******************************************************************************************************/ |
| 48 | + |
| 49 | +class Solution { |
| 50 | +private: |
| 51 | + void print(vector<vector<int>>& m) { |
| 52 | + int row = m.size(); |
| 53 | + int col = m[0].size(); |
| 54 | + for (int i=0; i<row; i++) { |
| 55 | + for(int j=0; j<col-1; j++) { |
| 56 | + cout << setw(3) << m[i][j] << ","; |
| 57 | + } |
| 58 | + cout << setw(3) << m[i][col-1] << endl; |
| 59 | + } |
| 60 | + cout << endl; |
| 61 | + } |
| 62 | +public: |
| 63 | + int kthLargestValue(vector<vector<int>>& matrix, int k) { |
| 64 | + int row = matrix.size(); |
| 65 | + int col = matrix[0].size(); |
| 66 | + vector<vector<int>> xmatrix(row, vector<int>(col, 0)); |
| 67 | + priority_queue<int> minHeap; |
| 68 | + |
| 69 | + xmatrix[0][0] = matrix[0][0]; |
| 70 | + minHeap.push(xmatrix[0][0]); |
| 71 | + |
| 72 | + for (int i=1; i<row; i++) { |
| 73 | + xmatrix[i][0] = xmatrix[i-1][0] ^ matrix[i][0]; |
| 74 | + minHeap.push(xmatrix[i][0]); |
| 75 | + } |
| 76 | + for (int i=1; i<col; i++) { |
| 77 | + xmatrix[0][i] = xmatrix[0][i-1] ^ matrix[0][i]; |
| 78 | + minHeap.push(xmatrix[0][i]); |
| 79 | + } |
| 80 | + |
| 81 | + for (int i=1; i<row; i++) { |
| 82 | + for(int j=1; j<col; j++) { |
| 83 | + xmatrix[i][j] = matrix[i][j] ^ |
| 84 | + xmatrix[i-1][j] ^ xmatrix[i][j-1] ^ xmatrix[i-1][j-1]; |
| 85 | + minHeap.push(xmatrix[i][j]); |
| 86 | + } |
| 87 | + } |
| 88 | + //print(matrix); |
| 89 | + //print(xmatrix); |
| 90 | + while( k-- > 1) { |
| 91 | + minHeap.pop(); |
| 92 | + } |
| 93 | + |
| 94 | + return minHeap.top(); |
| 95 | + } |
| 96 | +}; |
0 commit comments