首页 > 其他 > 详细

LeetCode Kth Smallest Element in a Sorted Matrix

时间:2017-02-02 11:06:47      阅读:180      评论:0      收藏:0      [点我收藏+]

原题链接在这里: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 public class Solution {
 2     public int kthSmallest(int[][] matrix, int k) {
 3         int n = matrix.length;
 4         
 5         PriorityQueue<Tuple> minHeap = new PriorityQueue<Tuple>();
 6         for(int j = 0; j<n; j++){
 7             minHeap.offer(new Tuple(0, j, matrix[0][j]));
 8         }
 9         while(--k>0){
10             Tuple t = minHeap.poll();
11             if(t.i == n-1){
12                 continue;
13             }
14             minHeap.offer(new Tuple(t.i+1, t.j, matrix[t.i+1][t.j]));
15         }
16         return minHeap.poll().val;
17     }
18 }
19 
20 class Tuple implements Comparable<Tuple>{
21     int i, j, val;
22     public Tuple(int i, int j, int val){
23         this.i = i;
24         this.j = j;
25         this.val = val;
26     }
27     
28     @Override
29     public int compareTo(Tuple that){
30         return this.val - that.val;
31     }
32 }

若采用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 = n-1;
21         int j = 0;
22         int count = 0;
23         
24         while(i>=0 && j<n){
25             if(matrix[i][j] <= mid){
26                 j++;
27                 count += i+1;
28             }else{
29                 i--;
30             }
31         }
32         return count;
33     }
34 }

 

LeetCode Kth Smallest Element in a Sorted Matrix

原文:http://www.cnblogs.com/Dylan-Java-NYC/p/6360951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!