zoukankan      html  css  js  c++  java
  • 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 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.

    题解:

    每一行每一列都是有序的,用一个minHeap先存入第一行,然后拿出minHeap顶部元素把它下方的元素放进minHeap中, 重复k-1次后, minHeap顶的就是第k小的元素.

    Time Complexity: O(n + klogn), heapify 用时O(n), (k-1)次poll 和 offer, 每次用时O(logn).

    Space: O(n), minHeap大小.

    AC Java:

     1 class Solution {
     2     public int kthSmallest(int[][] matrix, int k) {
     3         int n = matrix.length;
     4         PriorityQueue<int []> minHeap = new PriorityQueue<>((a, b) -> a[2] - b[2]);
     5         for(int j = 0; j < n; j++){
     6             minHeap.add(new int[]{0, j, matrix[0][j]});
     7         }
     8         
     9         while(--k > 0){
    10             int [] cur = minHeap.poll();
    11             if(cur[0] == n - 1){
    12                 continue;
    13             }
    14             
    15             minHeap.add(new int[]{cur[0] + 1, cur[1], matrix[cur[0] + 1][cur[1]]});
    16         }
    17         
    18         return minHeap.peek()[2];
    19     }
    20 }

    若采用Binary Search, 先猜一个值,这个值是由最小值matrix[0][0]和最大值matrix[n-1][n-1]求mid得来.

    然后算下小于等于这个mid的matrix元素个数,若是个数小于k, 就在mid到最大值之间再猜一个新的mid. 直到l 不再小于 r.

    Time Complexity: O(nlog(matrix[n-1][n-1] - matrix[0][0])), 每次lowerThanMidCount用时O(n).

    Space: O(1).

    AC Java:

     1 public class Solution {
     2     public int kthSmallest(int[][] matrix, int k) {
     3         int n = matrix.length;
     4         int l = matrix[0][0];
     5         int r = matrix[n-1][n-1];
     6         while(l < r){
     7             int mid = l + (r-l)/2;
     8             int temp = lowerThanMidCount(matrix, mid);
     9             if(temp < k){
    10                 l = mid+1;
    11             }else{
    12                 r = mid;
    13             }
    14         }
    15         return l;
    16     }
    17     
    18     private int lowerThanMidCount(int [][] matrix, int mid){
    19         int n = matrix.length;
    20         int i = 0;
    21         int j = n-1;
    22         int count = 0;
    23         
    24         while(i<n && j>=0){
    25             if(matrix[i][j] <= mid){
    26                 i++;
    27                 count += j+1;
    28             }else{
    29                 j--;
    30             }
    31         }
    32         return count;
    33     }
    34 }

    类似Find K Pairs with Smallest Sums.

  • 相关阅读:
    php使用cookie来保存用户登录信息
    Linux下进程操作
    TortoiseSVN无法编辑日志信息的解决方法
    用DIV布局制作公告板
    HashMap的遍历
    实现文本滚动
    子DIV块中设置margintop时影响父DIV块位置的解决办法
    php使用session来保存用户登录信息
    javascript jquery ajax动态提交多个参数 api测试 拂晓风起
    javascript 处理返回json中的\u中文乱码问题(也不是乱码了,就是\u编码) 拂晓风起
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6360951.html
Copyright © 2011-2022 走看看