forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path378_kthSmallest.java
38 lines (34 loc) · 1.04 KB
/
378_kthSmallest.java
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
37
38
这道题跟merge k sorted list是一类题目。
public class Solution {
public int kthSmallest(int[][] matrix, int k) {
Comparator<Tuple> comparator = new Comparator<Tuple>(){
public int compare(Tuple a, Tuple b){
return a.arr[a.start] - b.arr[b.start];
}
};
int n = matrix.length;
PriorityQueue<Tuple> pq = new PriorityQueue<Tuple>(n, comparator);
for(int[] m: matrix){
pq.offer(new Tuple(m, 0));
}
while(k > 0){
Tuple current = pq.remove();
k--;
if(k == 0) return current.arr[current.start];
if(current.start == current.arr.length-1) continue;
else{
current.start++;
pq.offer(current);
}
}
return -1;
}
public class Tuple{
public int[] arr;
public int start;
public Tuple(int[] arr, int start){
this.arr = arr;
this.start = start;
}
}
}