-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKthSmallest.java
More file actions
36 lines (32 loc) · 1.03 KB
/
KthSmallest.java
File metadata and controls
36 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.algorithm.stacksandqueues;
import java.util.PriorityQueue;
public class KthSmallest {
/**
* #378
* https://leetcode-cn.com/problems/kth-smallest-element-in-a-sorted-matrix/
*
* @param args
*/
public static void main(String[] args) {
int[][] matrix = new int[][] { { 1, 5, 9 }, { 10, 11, 13 }, { 12, 13, 15 } };
System.out.println(kthSmallest(matrix, 8));
int[][] matrix2 = new int[][] { { -5 } };
System.out.println(kthSmallest(matrix2, 1));
}
public static int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Integer> queue = new PriorityQueue<>(k, (a, b) -> b - a);
for (int[] nums : matrix) {
for (int num : nums) {
if (queue.size() < k) {
queue.offer(num);
continue;
}
if (num < queue.peek()) {
queue.poll();
queue.offer(num);
}
}
}
return queue.peek();
}
}