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,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.
这个题就我自己找到规律是:每个元素右下角的肯定比自己大,左上角的肯定比自己小,但是左下角和右上角的无法确定。

最简单的思路无非就是将数组中的元素都放在数组或List中,然后排序,找到第k小的即可。

public int kthSmallest(int[][] matrix, int k) {
    int i = 0;
    int[] list = new int[(matrix.length) * (matrix.length)]; 
    while(i< matrix.length){
        int j = 0;
        while(j< matrix.length){

            list[((i) * matrix.length)+j] = matrix[i][j];
            j++;
        }
        i++;
    }
    Arrays.sort(list);

    return list[k-1];
}

这个题可以用堆的思想和二分法来解。

如果用最大堆的话:

public int kthSmallest(int[][] matrix, int k) {  
    PriorityQueue<Integer> heap = new PriorityQueue<Integer>(new Comparator<Integer>() {  
        public int compare(Integer a0, Integer a1) {  
            if(a0>a1){  
                return -1;  
            }else if(a0<a1){  
                return 1;  
            }  
            return 0;  
        }  
    });// 最大堆  
    for (int i = 0; i < matrix.length; i++) {  
        for (int j = 0; j < matrix.length; j++) {  
            if (i * matrix.length + j + 1 > k) {  
                if (matrix[i][j] < heap.peek()) {  
                    heap.poll();  
                    heap.offer(matrix[i][j]);  
                }  
            } else {  
                heap.offer(matrix[i][j]);  
            }  
        }  
  
    }  
    return heap.peek();  
}

 只要能想到用堆来解决,这个思路就很容易出来。 

下面用最小堆的更加巧妙:

将每列最小的元素放进堆中,然后查找每一列下面的元素加入堆,每次取出堆中的元素都是最小的。代码如下:

class Tuple implements Comparable<Tuple> {
    int x, y, val;
    public Tuple (int x, int y, int val) {
        this.x = x;
        this.y = y;
        this.val = val;
    }
    
    @Override
    public int compareTo (Tuple that) {
        return this.val - that.val;
    }
}

public int kthSmallest(int[][] matrix, int k) {
    int n = matrix.length;
    PriorityQueue<Tuple> pq = new PriorityQueue<Tuple>();
    for(int j = 0; j <= n-1; j++){
    	System.out.println(matrix[0][j]);
    	pq.offer(new Tuple(0, j, matrix[0][j]));
    }
    for(int i = 0; i < k-1; i++) {
        Tuple t = pq.poll();
        System.out.println(t.val+"------");
        if(t.x == n-1) {
        	continue;
        }
        System.out.println(matrix[t.x+1][t.y]+"****");
        pq.offer(new Tuple(t.x+1, t.y, matrix[t.x+1][t.y]));
    }
    return pq.poll().val;
}

二分查找的具体做法如下:

public int kthSmallest(int[][] matrix, int k)  {
    int matrixRowSize=matrix.length;
    int matrixColSize=matrix[0].length;
    int minVal = matrix[0][0];
    int maxVal = matrix[matrixRowSize-1][matrixColSize-1];
    int midVal = 0;
    int count = 0;
    while (minVal < maxVal) {
        midVal = (minVal + maxVal) / 2;
        for (int i  = 0; i < matrixRowSize && matrix[i][0] <= midVal; i++) {
            for (int j = 0; j < matrixColSize; j++) {
                if (matrix[i][j] <= midVal) {
                    count++;   
                }
            }
        }
        if (k <= count) {
            maxVal = midVal;
        } else {
            minVal = midVal + 1;
        }
        count = 0;
    }
    return minVal;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值