Skip to content

Commit 92668e9

Browse files
author
robot
committed
go
1 parent 341f4e8 commit 92668e9

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

problems/378.kth-smallest-element-in-a-sorted-matrix.md

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,25 @@ k = 8,
4040

4141
## 思路
4242

43-
显然用大顶堆可以解决,时间复杂度 Klogn n 为总的数字个数,
44-
但是这种做法没有利用题目中 sorted matrix 的特点,因此不是一种好的做法.
43+
显然用大顶堆可以解决,时间复杂度 Klogn,其中 n 为矩阵中总的数字个数。但是这种做法没有利用题目中 sorted matrix 的特点(横向和纵向均有序),因此不是一种好的做法.
4544

46-
一个巧妙的方法是二分法,我们分别从第一个和最后一个向中间进行扫描,并且计算出中间的数值与数组中的进行比较,
47-
可以通过计算中间值在这个数组中排多少位,然后得到比中间值小的或者大的数字有多少个,然后与 k 进行比较,如果比 k 小则说明中间值太小了,则向后移动,否则向前移动。
45+
一个巧妙的方法是二分法,我们分别从第一个和最后一个向中间进行扫描,并且计算出中间的数值与数组中的进行比较,可以通过计算中间值在这个数组中排多少位,然后得到比中间值小的或者大的数字有多少个,然后与 k 进行比较,如果比 k 小则说明中间值太小了,则向后移动,否则向前移动。
4846

4947
这个题目的二分确实很难想,我们来一步一步解释。
5048

51-
最普通的二分法是有序数组中查找指定值(或者说满足某个条件的值)。由于是有序的,我们可以根据索引关系来确定大小关系,
52-
因此这种思路比较直接,但是对于这道题目索引大小和数字大小没有直接的关系,因此这种二分思想就行不通了。
49+
最普通的二分法是有序数组中查找指定值(或者说满足某个条件的值)这种思路比较直接,但是对于这道题目是二维矩阵,而不是一维数组,因此这种二分思想就行不通了。
5350

5451
![378.kth-smallest-element-in-a-sorted-matrix-1](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyc87pwj30gb03u0sx.jpg)
5552

56-
(普通的基于索引判断的二分法)
53+
(普通的一维二分法)
5754

58-
- 我们能够找到矩阵中最大的元素(右下角)和最小的元素(左上角)。我们可以求出值的中间,而不是上面那种普通二分法的索引的中间。
55+
而实际上:
56+
57+
- 我们能够找到矩阵中最大的元素(右下角)和最小的元素(左上角)。接下来我们可以求出**值的中间**,而不是上面那种普通二分法的索引的中间。
5958

6059
![378.kth-smallest-element-in-a-sorted-matrix-3](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyd2629j30ch05faaa.jpg)
6160

62-
- 找到中间值之后,我们可以拿这个值去计算有多少元素是小于等于它的。
63-
具体方式就是比较行的最后一列,如果中间值比最后一列大,说明中间元素肯定大于这一行的所有元素。 否则我们从后往前遍历直到不大于。
61+
- 找到中间值之后,我们可以拿这个值去计算有多少元素是小于等于它的。具体方式就是比较行的最后一列,如果中间值比最后一列大,说明中间元素肯定大于这一行的所有元素。 否则我们从后往前遍历直到不大于。
6462

6563
![378.kth-smallest-element-in-a-sorted-matrix-2](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyeslbij30by06awep.jpg)
6664

@@ -78,27 +76,11 @@ k = 8,
7876

7977
![378.kth-smallest-element-in-a-sorted-matrix-4](https://tva1.sinaimg.cn/large/007S8ZIlly1ghltyfm2okj30je0fq0uf.jpg)
8078

81-
这里有一个大家普遍都比较疑惑的点,也是我当初非常疑惑,困扰我很久的点, leetcode 评论区也有很多人来问,就是“能够确保最终我们找到的元素一定在矩阵中么?”
82-
83-
答案是可以, `相等的时候一定在matrix里面。 因为原问题一定有解,找下界使得start不断的逼近于真实的元素`.
84-
85-
我是看了评论区一个大神的评论才明白的,以下是[@GabrielaSong](https://leetcode.com/gabrielasong/)的评论原文:
86-
87-
```
88-
The lo we returned is guaranteed to be an element in the matrix is because:
89-
Let us assume element m is the kth smallest number in the matrix, and x is the number of element m in the matrix.
90-
When we are about to reach convergence, if mid=m-1, its count value (the number of elements which are <= mid) would be k-x,
91-
so we would set lo as (m-1)+1=m, in this case the hi will finally reach lo;
92-
and if mid=m+1, its count value would be k+x-1, so we would set hi as m+1, in this case the lo will finally reach m.
93-
To sum up, because the number lo found by binary search find is exactly the element which has k number of elements in the matrix that are <= lo,
94-
The equal sign guarantees there exists and only exists one number in range satisfying this condition.
95-
So lo must be the only element satisfying this element in the matrix.
96-
97-
```
79+
这里有一个大家普遍都比较疑惑的点,就是“能够确保最终我们找到的元素一定在矩阵中么?”
9880

99-
更多解释,可以参考[leetcode discuss](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/discuss/85173/Share-my-thoughts-and-Clean-Java-Code)
81+
答案是可以, **因为我们可以使用最左二分,这样假设我们找到的元素不在矩阵,那么我们一定可以找到比它小的在矩阵中的值,这和我们的假设(最左二分)矛盾**
10082

101-
> 如果是普通的二分查找,我们是基于索引去找,因此不会有这个问题
83+
不懂最左二分请看我的二分专题
10284

10385
## 关键点解析
10486

@@ -173,6 +155,4 @@ var kthSmallest = function (matrix, k) {
173155

174156
- [240.search-a-2-d-matrix-ii](./240.search-a-2-d-matrix-ii.md)
175157

176-
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
177-
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
178-
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)
158+
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 47K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)