leetcode 378. 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.

这道题我直接是暴力求解。

建议和leetcode 240. Search a 2D Matrix II 矩阵搜索 + 右上角搜索leetcode 668. Kth Smallest Number in Multiplication Table 有序矩阵搜索

代码如下:

import java.util.Arrays;

class Solution 
{
    public int kthSmallest(int[][] matrix, int k) 
    {
        int[] num=new int[matrix.length*matrix[0].length];
        int count=0;
        for(int i=0;i<matrix.length;i++)
        {
            for(int j=0;j<matrix[0].length;j++)
            {
                num[count++]=matrix[i][j];
            }
        }
        Arrays.sort(num);
        return num[k-1];
    }
}

下面是C++的做法,直接使用堆去遍历即可

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

本题的考察的本意是二分查找

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;



class Solution
{
public:
    int kthSmallest(vector<vector<int>>& mat, int k)
    {
        int row = mat.size(), col = mat[0].size();
        int left = mat[0][0], right = mat[row - 1][col - 1];
        while (left < right)
        {
            int mid = (right - left) / 2 + left;
            int count = lessMid(mat, mid);
            if (count<k)
                left = mid + 1;
            else
                right = mid;

        }
        return left;
    }

    int lessMid(vector<vector<int>>& mat, int target)
    {
        int res = 0;
        int row = mat.size(), col = mat[0].size();
        int i = 0, j = col - 1;

        while (i < row && j >= 0)
        {
            if (mat[i][j] <= target)
            {
                res += (j + 1);
                i++;
            }
            else
                j--;
        }
        return res;
    }


    int kthSmallestByQueue(vector<vector<int>>& mat, int k)
    {
        priority_queue<int> que;
        for (int i = 0; i < mat.size(); i++)
        {
            for (int j = 0; j < mat[0].size(); j++)
            {
                que.push(mat[i][j]);
                if (que.size() > k)
                    que.pop();
            }
        }
        return que.top();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值