[LeetCode] 378. Kth Smallest Element in a Sorted Matrix

题:https://leetcode.com/problems/insert-delete-getrandom-o1/description/

题目

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.

思路

题目大意

给定一个 n x n 的矩阵,矩阵的每行每列都是升序的。求第k个小的元素哪个?

解题思路

方法一 利用 最小堆 直接求解

将第k个小 问题 转变为 第 n * n - k + 1 个大。
设立一个最小堆,将 matrix中所有的元素依次填入,若堆的size() 大于n*n - k + 1 个,剔除最小的元素。

或设立 一个 最大堆,将matirx 中所有元素依次填入,若堆的size() 大于 k 个,剔除最大元素。

方法二 利用 matrix 特性 与 最小堆

主要思想是剔除前 k-1 个 最小值。

将 数组 matrix[0] 放入 最小堆中,pop()最小值,若找出该元素下一行的元素放入 最小堆中,循环 k-1次。最后返回 最小堆的 首元素。

方法三 二分法 、upper_bound、lower_bound 函数

二分法,设 matrix 中最小数 matrix[0][0] ,最大数 matrix[len-1][len-1] 。

每次,求中间值 在 matrix 中 大于等于的个数 cnt。即对每行 使用upper_bound 函数。

求最小中间值 使得 cnt == k,类似 lower_bound 算法。

code

方法一 利用 最小堆 直接求解

class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        Queue<Integer> priorityQueue = new PriorityQueue<>();
        int reverse_numslen = matrix.length * matrix[0].length - k;

        for (int i = 0; i < matrix.length; i++)
            for (int j = 0; j < matrix[0].length; j++) {
                priorityQueue.add(matrix[i][j]);
                if (priorityQueue.size() > reverse_numslen +1){
                    priorityQueue.poll();
                }
            }
        
        return priorityQueue.peek();
    }
}

方法二 利用 matrix 特性 与 最小堆


class matrixEle{
    int val;
    int x,y;
    public matrixEle(int val,int x,int y){
        this.val = val;
        this.x = x;
        this.y = y;
    }
}


class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        Queue<matrixEle> priorityQueue = new PriorityQueue<>(new Comparator<matrixEle>() {
            @Override
            public int compare(matrixEle o1, matrixEle o2) {
                return o1.val - o2.val;
            }
        });
        int matrixlen = matrix.length;
        for(int i = 0 ;i<matrixlen;i++)
            priorityQueue.add(new matrixEle(matrix[0][i],0,i));
        for(int j = 0;j<k-1;j++){
            matrixEle cur = priorityQueue.poll();
            if(cur.x +1 < matrixlen) priorityQueue.add(new matrixEle(matrix[cur.x+1][cur.y],cur.x+1,cur.y));
        }
        return priorityQueue.peek().val;
    }
}

方法三 利用 matrix 特性 与 upper_bound 函数

class Solution {
    public static int upperBound(int []nums,int leftloc,int rightloc,int val){
        while(leftloc<rightloc){
            int midloc = (leftloc + rightloc)/2;
            if(nums[midloc]<= val ) leftloc = midloc + 1 ;
            else  rightloc = midloc;
        }
        return leftloc;
    }

    public int kthSmallest(int[][] matrix, int k) {
        int matrixlen = matrix.length;
        int leftval = matrix[0][0],rightval = matrix[matrixlen - 1][matrixlen - 1];
        while(leftval<rightval){
            int midval = (leftval + rightval)/2;
            int cnt = 0;
            for( int i = 0 ; i< matrixlen;i++){
                cnt += Solution.upperBound(matrix[i],0,matrixlen,midval);
            }
            if(cnt<k)   leftval = midval + 1;
            else rightval = midval;
        }
        return rightval;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值