Skip to content

Commit dcf0a5a

Browse files
authored
Update 1738-find-kth-largest-xor-coordinate-value.js
1 parent 975b098 commit dcf0a5a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1738-find-kth-largest-xor-coordinate-value.js

+30
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,34 @@ var kthLargestValue = function(matrix, k) {
2727
return v[k - 1];
2828
};
2929

30+
// another
31+
32+
/**
33+
* @param {number[][]} matrix
34+
* @param {number} k
35+
* @return {number}
36+
*/
37+
const kthLargestValue = function(matrix, k) {
38+
const tmp = []
39+
const n = matrix.length, m = matrix[0].length
40+
const dp = Array.from({ length: n }, () => Array(m).fill(0))
41+
dp[0][0] = matrix[0][0]
42+
tmp.push(dp[0][0])
43+
for(let j = 1; j < m; j++) {
44+
dp[0][j] = dp[0][j - 1] ^ matrix[0][j]
45+
tmp.push(dp[0][j])
46+
}
47+
for(let i = 1; i < n; i++) {
48+
dp[i][0] = dp[i - 1][0] ^ matrix[i][0]
49+
tmp.push(dp[i][0])
50+
}
51+
for(let i = 1; i < n; i++) {
52+
for(let j = 1; j < m; j++) {
53+
dp[i][j] = dp[i][j - 1] ^ dp[i - 1][j] ^ matrix[i][j] ^ dp[i - 1][j - 1]
54+
tmp.push(dp[i][j])
55+
}
56+
}
57+
tmp.sort((a, b) => b - a)
58+
return tmp[k - 1]
59+
};
3060

0 commit comments

Comments
 (0)