Skip to content

Commit 6ef88aa

Browse files
committed
New Problem Solution -"1738. Find Kth Largest XOR Coordinate Value"
1 parent 1e87411 commit 6ef88aa

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ LeetCode
5656
|1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [C++](./algorithms/cpp/sumOfUniqueElements/SumOfUniqueElements.cpp)|Easy|
5757
|1743|[Restore the Array From Adjacent Pairs](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/) | [C++](./algorithms/cpp/restoreTheArrayFromAdjacentPairs/RestoreTheArrayFromAdjacentPairs.cpp)|Medium|
5858
|1742|[Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/) | [C++](./algorithms/cpp/maximumNumberOfBallsInABox/MaximumNumberOfBallsInABox.cpp)|Easy|
59+
|1738|[Find Kth Largest XOR Coordinate Value](https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/) | [C++](./algorithms/cpp/findKthLargestXorCoordinateValue/FindKthLargestXorCoordinateValue.cpp)|Medium|
5960
|1736|[Latest Time by Replacing Hidden Digits](https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/) | [C++](./algorithms/cpp/latestTimeByReplacingHiddenDigits/LatestTimeByReplacingHiddenDigits.cpp)|Easy|
6061
|1734|[Decode XORed Permutation](https://leetcode.com/problems/decode-xored-permutation/) | [C++](./algorithms/cpp/decodeXORedPermutation/DecodeXoredPermutation.cpp)|Medium|
6162
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)