Kth Smallest Number in Sorted Matrix

Find the kth smallest number in at row and column sorted matrix.

Example

Given k = 4 and a matrix:

[
  [1 ,5 ,7],
  [3 ,7 ,8],
  [4 ,8 ,9],
]

return 5.

分析:

Although the matrix is horizontally and vertically sorted, there is no rule to find the next smallest number. In this solution, we will put the numbers in the first row to a heap, and remove the smallest one and add a new number whose position is right under the removed one.

Note: when requesting to find the kth largest/smallest number, you should consider heap sort.

 1 public class Solution {
 2     /**
 3      * @param matrix: a matrix of integers
 4      * @param k: an integer
 5      * @return: the kth smallest number in the matrix
 6      */
 7     public int kthSmallest(int[][] matrix, int k) {
 8         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
 9             return 0;
10         }
11         if (k > matrix.length * matrix[0].length) {
12             return 0;
13         }
14         return vertical(matrix, k);
15     }
16
17     private int vertical(int[][] matrix, int k) {
18         Queue<Point> heap = new PriorityQueue<Point>(k, new Comparator<Point>() {
19             public int compare(Point left, Point right) {
20                 return left.val - right.val;
21             }
22         });
23         for (int i = 0; i < Math.min(matrix[0].length, k); i++) {
24             heap.offer(new Point(0, i, matrix[0][i]));
25         }
26         for (int i = 1; i <= k -1; i++) {
27             Point curr = heap.poll();
28             if (curr.row + 1 < matrix.length) {
29                 heap.offer(new Point(curr.row + 1, curr.col, matrix[curr.row + 1][curr.col]));
30             }
31         }
32         return heap.peek().val;
33     }
34 }
35
36 class Point {
37         public int row, col, val;
38         public Point(int row, int col, int val) {
39             this.row = row;
40             this.col = col;
41             this.val = val;
42         }
43     } 
时间: 2024-10-12 01:05:53

Kth Smallest Number in Sorted Matrix的相关文章

[Lintcode] Kth Smallest Number in Sorted Matrix

Kth Smallest Number in Sorted Matrix Find the kth smallest number in at row and column sorted matrix. Have you met this question in a real interview? Yes Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k lo

Kth Smallest Element in Sorted Matrix

1 public class Solution { 2 public int kthSmallest(int[][] matrix, int k) { 3 if (matrix.length == 0 || matrix[0].length == 0 || k <= 0) { 4 return -1; 5 } 6 PriorityQueue<PayLoad> queue = new PriorityQueue<>(); 7 for (int i = 0; i < mat

668. Kth Smallest Number in Multiplication Table

Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table? Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to ret

[LeetCode] Kth Smallest Number in Multiplication Table 乘法表中的第K小的数字

Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table? Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to ret

[Leetcode]668.Kth Smallest Number in Multiplication Table

链接:LeetCode668 给定高度m?.宽度n 的一张?m * n的乘法表,以及正整数k,你需要返回表中第k?小的数字. 例?1: 输入: m = 3, n = 3, k = 5 输出: 3 解释: 乘法表: 1 2 3 2 4 6 3 6 9 第5小的数字是 3 (1, 2, 2, 3, 3). 相关标签:二分查找 乘法表的特点是每行和每列元素均按升序排序,这题就可以转换为[LeetCode]378.Kth Smallest Element in a Sorted Matrix.我们通过二

Kth Smallest Sum In Two Sorted Arrays

Given two integer arrays sorted in ascending order and an integer k. Define sum = a + b, where a is an element from the first array and b is an element from the second one. Find the kth smallest sum out of all possible sums. Given [1, 7, 11] and [2,

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 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

【Leetcode】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 eleme

Kth Smallest Element in a Sorted Matrix -- LeetCode

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