[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.

Note:
You may assume k is always valid, 1 ≤ k ≤ n2.

这道题让我们求有序矩阵中第K小的元素,这道题的难点在于数组并不是蛇形有序的,意思是当前行的最后一个元素并不一定会小于下一行的首元素,所以我们并不能直接定位第K小的元素,所以只能另辟蹊径。先来看一种利用堆的方法,我们使用一个最小堆,然后遍历数组每一个元素,将其加入堆,根据最小堆的性质,小的元素会排到最前面,然后我们看当前堆中的元素个数是否大于k,大于的话就将末尾的元素去掉,循环结束后我们返回堆中的首元素即为所求:

解法一:

class Solution {
public:
    int kthSmallest(vector<vector<int>>& matrix, int k) {
        priority_queue<int, vector<int>> q;
        for (int i = 0; i < matrix.size(); ++i) {
            for (int j = 0; j < matrix[i].size(); ++j) {
                q.emplace(matrix[i][j]);
                if (q.size() > k) q.pop();
            }
        }
        return q.top();
    }
};

这题我们也可以用二分查找法来做,我们由于是有序矩阵,那么左上角的数字一定是最小的,而右下角的数字一定是最大的,所以这个是我们搜索的范围,然后我们算出中间数字mid,由于矩阵中不同行之间的元素并不是严格有序的,所以我们要在每一行都查找一下mid,我们使用upper_bound,这个函数是查找第一个大于目标数的元素,如果目标数在比该行的尾元素大,则upper_bound返回该行元素的个数,如果目标数比该行首元素小,则upper_bound返回0, 我们遍历完所有的行可以找出中间数是第几小的数,然后k比较,进行二分查找,参见代码如下:

解法二:

class Solution {
public:
    int kthSmallest(vector<vector<int>>& matrix, int k) {
        int left = matrix[0][0], right = matrix.back().back();
        while (left < right) {
            int mid = left + (right - left) / 2, cnt = 0;
            for (int i = 0; i < matrix.size(); ++i) {
                cnt += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin();
            }
            if (cnt < k) left = mid + 1;
            else right = mid;
        }
        return left;
    }
};

参考资料:

https://discuss.leetcode.com/topic/52857/c-priority_queue-ac-implementation

https://discuss.leetcode.com/topic/52865/my-solution-using-binary-search-in-c

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-08-01 22:47:32

[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素的相关文章

378 Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素.示例:matrix = [   [ 1,  5,  9],   [10, 11, 13],   [12, 13, 15]],k = 8,返回 13.说明:你可以假设 k 的值永远是有效的, 1 ≤ k ≤ n2 .详见:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/des

leetcode.矩阵.378有序矩阵中第K小的元素-Java

1. 具体题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1,  5,  9], [10, 11, 13], [12, 13, 15] ],k = 8, 返回 13. 2. 思路分析 二分法:初始化 low 指针为矩阵中最小元素:matrix[0][0],high 指针为矩阵中最大元素:matrix[rows-1][cols-1],之后进行二分查找,迭代时每次计算矩阵中

230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你需要频繁地找到第k小值呢? 你将如何优化kthSmallest函数? 详见:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 方法一:递归实现 /** * Definition for a binary

【Leetcode】378. Kth Smallest Element in a Sorted Matrix

Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix

378. Kth Smallest Element in a Sorted Matrix(java,优先队列)

题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Example: matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8, return 13. 分析:采用优先队列,自定义比较规则,使得最小元素排在队首. 代码:

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest eleme

leetcode 378. Kth Smallest Element in a Sorted Matrix

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

Kth Smallest Element in a Sorted Matrix -- LeetCode

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. Example: matrix = [ [ 1, 5

378. Kth Smallest Element in a Sorted Matrix

https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/#/solutions http://www.cnblogs.com/EdwardLiu/p/6109080.html Heap : public class Solution { public int kthSmallest(int[][] matrix, int k) { int n = matrix.length; PriorityQueue<Tupl