LeetCode 378. 有序矩阵中第K小的元素

news/2024/5/20 10:33:27 标签: 二分查找,

原题目:https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/

 

思路一:

使用大顶,找出前k小。

 

代码:

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

 

思路二:

二分查找,找出小于mid的数目,如果此数目大于等于k,那么就在小于mid的数里面继续找,否则在大于 mid的数里面继续寻找。

数目怎么查找?我们可以分析发现,因为每行每列都是升序的,所以小于mid的数一定位于其左上角,则如下图所示(来源于leetcode):

 

class Solution {
public:
    int count(vector<vector<int>>& matrix, int n ,int c){
        int num = 0;
        int i = n-1,j=0;
        while(i>=0 && j<n){
            if(matrix[i][j]<=c){
                num += i+1;
                j++;
            }
            else{
                i--;
            }
        }
        return num;
    }
    int kthSmallest(vector<vector<int>>& matrix, int k) {
        int n = matrix.size();
        int left = matrix[0][0];
        int right = matrix[n-1][n-1];
        while(left<=right){
            int mid = left + (right - left) / 2;
            if(count(matrix,n,mid) >= k) right = mid - 1;
            else left = mid + 1;
        }
        return left;
    }
};

 


http://www.niftyadmin.cn/n/899036.html

相关文章

LeetCode 455. 分发饼干

原题目&#xff1a;https://leetcode-cn.com/problems/assign-cookies/ 思路&#xff1a; 对数组进行排序&#xff0c;然后使用贪心法即可&#xff0c;找到可以给孩子的饼干就给他。 如果s[j] > g[i] i。 代码&#xff1a; class Solution { public:int findContentChild…

LeetCode 134. 加油站

原题目&#xff1a;https://leetcode-cn.com/problems/gas-station/ 思路&#xff1a; 如果可以绕行一圈&#xff0c;那么需要满足两个条件 1、车子可以从i开到i1&#xff0c;即车子开到i1使得油tmp≥0 2、全程油的量要大于耗费的量 sum ≥ cost 实现&#xff1a; 用ai表示…

LeetCode 860. Lemonade Change

原题目&#xff1a;https://leetcode-cn.com/problems/lemonade-change/ 思路&#xff1a; 因为只有5/10/20三种情况&#xff0c;我们可以对三种情况分别进行判断。 我们使用five和ten记录5块和10块的张数&#xff0c;用于找零钱 代码&#xff1a; class Solution { public:…

LeetCode 435. Non-overlapping Intervals

原题目&#xff1a;https://leetcode-cn.com/problems/non-overlapping-intervals/ 思路&#xff1a; 首先根据起点对数据进行排序&#xff0c;分析可知&#xff0c;会出现两种情况 1、后一个的起点大于等于前一个的重点&#xff0c;此时不需要删除节点&#xff0c;用left记住…

LeetCode 44. Wildcard Matching

原题目&#xff1a;https://leetcode-cn.com/problems/wildcard-matching/ 思路&#xff1a; 本题可以使用动态规划的思想求解&#xff0c;动态规划有两个步骤&#xff1a;找出转移方程&#xff0c;确定初始&#xff08;边界&#xff09;条件 我们使用dp[i][j]来表示s中前i个…

LeetCode 874. Walking Robot Simulation

原题目&#xff1a;https://leetcode-cn.com/problems/walking-robot-simulation/ 思路&#xff1a; 用0,1,2,3模拟走的方向&#xff0c;分别代表北东南西&#xff0c;用map存储障碍的位置&#xff0c;每次移步之前&#xff0c;先判断下一步是不是障碍&#xff0c;如果是就不在…

LeetCode 944. Delete Columns to Make Sorted

原题目&#xff1a;https://leetcode-cn.com/problems/delete-columns-to-make-sorted/ 思路&#xff1a; 本题的意思是矩阵中有多少列是升序的&#xff0c;所以我们对每一列进行遍历&#xff0c;如果相邻两个是降序就应该删除该列。 代码&#xff1a; class Solution { publ…

LeetCode 406. Queue Reconstruction by Height

原题目&#xff1a;https://leetcode-cn.com/problems/queue-reconstruction-by-height/ 思路&#xff1a; 这题使用到贪心算法&#xff0c; 具体的贪心策略为&#xff1a;按身高从大到小依次插入&#xff0c;因为后面插入的元素肯定不大于前面的元素&#xff0c;所以插入的位…