Problem
You are given a 2D matrix
of size m x n
, consisting of non-negative integers. You are also given an integer k
.
The value of coordinate (a, b)
of the matrix is the XOR of all matrix[i][j]
where 0 <= i <= a < m
and 0 <= j <= b < n
(0-indexed).
Find the kth
largest value (1-indexed) of all the coordinates of matrix
.
Example 1:
1 | Input: matrix = [[5,2],[1,6]], k = 1 |
Example 2:
1 | Input: matrix = [[5,2],[1,6]], k = 2 |
Example 3:
1 | Input: matrix = [[5,2],[1,6]], k = 3 |
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
0 <= matrix[i][j] <= 106
1 <= k <= m * n
Analysis
这道题目给出一个矩阵,定义每个位置的value
是以它和左上角为顶点的矩阵内所有元素的异或结果。对于矩阵中,要求子矩阵的和、异或结果等,优先考虑的就是矩阵前缀和。其基本公式为preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] + preSum[i][j] - preSum[i - 1][j - 1]
。
这道题目基本就按照上面的公式就能快速求得每个位置的value,然后是求第k大的value。求第k
大数一般有两种方法,一个是用priority queue,另一种是类似于快排的方法。这里因为value是随着不断产生的,更加适合用priority queue,所以我们维护前k
个元素即可。
Solution
无。
Code
1 | class Solution { |
Summary
这道题目是矩阵preSum加求第k
大数两个知识点的结合,都不难。这道题目的分享到这里,感谢你的支持!