关闭

Kth Smallest Element in a Sorted Matrix

标签: leetcode二分查找大小堆
502人阅读 评论(0) 收藏 举报
分类:

题目描述:

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
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

[leetcode] 378. Kth Smallest Element in a Sorted Matrix 解题报告

题目链接: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ Given a n x n matrix ...
  • qq508618087
  • qq508618087
  • 2016-08-20 15:43
  • 1343

LeetCode 230: Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not...
  • sunao2002002
  • sunao2002002
  • 2015-07-02 14:27
  • 5543

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n ...
  • yeqiuzs
  • yeqiuzs
  • 2016-08-01 21:54
  • 3117

LeetCode—378. Kth Smallest Element in a Sorted Matrix

Kth Smallest Element in a Sorted Matrix思路:1.首先这题把所有的数组遍历出来然后直接排序就可以AC..... 2.正常思路就是维系最大堆。 ...
  • corpsepiges
  • corpsepiges
  • 2016-08-02 10:20
  • 2105

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-28 06:57
  • 4120

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respe...
  • wangxiaojun911
  • wangxiaojun911
  • 2014-02-06 03:32
  • 6334

寻找数组中前K个最小的数(Kth smallest element)---(堆排序的应用)

日常生活中我们总是遇到求解一个数组的最小值或者最大值等问题,这些问题基本上都可以在
  • shaya118
  • shaya118
  • 2014-11-07 14:48
  • 1134

378. Kth Smallest Element in a Sorted Matrix-binary search/heap

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
  • u011567017
  • u011567017
  • 2017-02-18 11:56
  • 133

LeetCode:378 Kth Smallest Element in a Sorted Matrix

378. Kth Smallest Element in a Sorted MatrixGiven a n x n matrix where each of the rows and columns ...
  • Wisimer
  • Wisimer
  • 2016-08-13 23:33
  • 227

【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...
  • xiaoquantouer
  • xiaoquantouer
  • 2016-08-06 13:21
  • 1599
    个人资料
    • 访问:57329次
    • 积分:2715
    • 等级:
    • 排名:第15309名
    • 原创:215篇
    • 转载:37篇
    • 译文:0篇
    • 评论:6条
    最新评论