leetcode-378-kth-smallest-element-in-a-sorted-matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/  

Given a <i>n</i> x <i>n</i> 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.

<b>Example:</b>
<pre>matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.
</pre>
<b>Note: </b>
You may assume k is always valid, 1 ≤ k ≤ n<sup>2</sup>.

 

给一个n*n的二维数组matrix , 它的所有行和列都是升序排列。求这个矩阵中第k小的数。    

将所给数据的最小值和最大值作为low 和high 。 搜索判断mid值是否满足第k小的条件。    本题由于行列都有序。判断时可以加快速度。  复杂度o((m+n)*logn)  

    class Solution {
       public int kthSmallest(int[][] matrix, int k) {
          int n = matrix.length;
          int m = matrix[0].length;
           
           int low = matrix[0][0];   //最小值
           int high = matrix[n-1][m-1];  //最大值
           while(low <= high){
               int mid = low + (high-low)/2;
              //判断是否满足条件部分
                int count = 0;
               int j = m-1;
               for(int i = 0; i < n; i++){
                   while(j >= 0 && matrix[i][j] > mid)  //由于列是按升序排列,j可以不必每次从数组尾部开始减
                       j--;
                   count += (j+1);    //count 表示值小于等于mid的个数
              }
               if(count < k)   
                   low = mid+1;
               else    
                   high = mid-1;   // count==k 说明mid是第k大的值,但是mid可能不在原数组中。所以不能直接返回mid。应继续搜索
           }
           return low;
      }
    }
    public class Solution {
       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++) 
               pq.offer(new Tuple(0, j, matrix[0][j]));
           for(int i = 0; i < k-1; i++) {
               Tuple t = pq.poll();
               if(t.x == n-1) continue;
               pq.offer(new Tuple(t.x+1, t.y, matrix[t.x+1][t.y]));
           }
           return pq.poll().val;
       }
    }

    class Tuple implements Comparable<Tuple> {
       //需要比较,实现comparable接口中的conpareTo方法
       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;  
       }
    }
0

Powered by Jekyll and Theme by solid