lc378.java 1.9 KB
Newer Older
L
liu13 已提交
1
package code;
L
liu13 已提交
2 3 4 5

import java.util.Comparator;
import java.util.PriorityQueue;

L
liu13 已提交
6 7 8 9 10
/*
 * 378. Kth Smallest Element in a Sorted Matrix
 * 题意:在矩阵中搜索第k大的数,横轴和纵轴都是有序的
 * 难度:Medium
 * 分类:Binary Search, Heap
L
liu13 已提交
11 12
 * 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
 *                2是二分,二分的是val,看比这个val小的数是不是k
L
liu13 已提交
13
 * Tips:lc23方法很像
L
liu13 已提交
14
 *       lc240, lc378
L
liu13 已提交
15 16
 */
public class lc378 {
L
liu13 已提交
17 18 19 20 21 22 23 24
    class Cell{
        int val, row, col;
        Cell(int v, int r, int c){
            val = v;
            row = r;
            col = c;
        }
    }
L
liu13 已提交
25
    public int kthSmallest(int[][] matrix, int k) {
L
liu13 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        PriorityQueue<Cell> pq = new PriorityQueue(new Comparator<Cell>() {
            @Override
            public int compare(Cell o1, Cell o2) {
                return o1.val-o2.val;
            }
        });
        for (int i = 0; i < matrix.length ; i++) pq.add(new Cell(matrix[i][0], i, 0));
        while(k>1){
            Cell c = pq.remove();
            if(c.col+1<matrix[0].length) pq.add(new Cell(matrix[c.row][c.col+1], c.row, c.col+1));
            k--;
        }
        return pq.remove().val;
    }

    public int kthSmallest2(int[][] matrix, int k) {
        int low = matrix[0][0];
        int high = matrix[matrix.length-1][matrix[0].length-1];
        while(low<=high){   //确保区间最后缩到0
            int mid = low+(high-low)/2;
            int num = getLessNum(matrix, mid);
            if(num<k) low = mid+1;
            else high = mid-1;
        }
        return low-1;
    }
    public int getLessNum(int[][] matrix, int val){ //求矩阵中比这个数小的数的个数
        int res = 0;
        int row = 0;
        while(row<matrix.length){
            int col = 0;
            while(col<matrix[0].length && matrix[row][col]<val) col++;
            res += col;
            row++;
        }
        return res;
L
liu13 已提交
62 63
    }
}