-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
63 lines (58 loc) · 1.54 KB
/
main.rs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
fn main() {
assert_eq!(Solution::kth_smallest(vec![
vec![ 1, 5, 9],
vec![10, 11, 13],
vec![12, 13, 15]
], 8), 13);
}
struct Solution {}
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pub fn kth_smallest(mut matrix: Vec<Vec<i32>>, k: i32) -> i32 {
let mut min_heap = BinaryHeap::new();
let l = matrix.len();
for _ in (0..l).rev() {
min_heap.push(Reverse(matrix.pop().unwrap()));
}
let mut cnt = 0;
let mut res = 0;
while cnt < k as usize {
let mut x = min_heap.pop().unwrap().0;
res = x.remove(0);
if !x.is_empty() { min_heap.push(Reverse(x)) }
cnt += 1
}
res
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::kth_smallest(vec![
vec![ 1, 5, 9],
vec![10, 11, 13],
vec![12, 13, 15]
], 8), 13);
assert_eq!(Solution::kth_smallest(vec![
vec![ 1, 5, 9],
vec![10, 11, 13],
vec![12, 13, 15]
], 6), 12);
assert_eq!(Solution::kth_smallest(vec![
vec![ 1, 5, 9],
vec![10, 11, 13],
vec![12, 13, 15]
], 7), 13);
assert_eq!(Solution::kth_smallest(vec![
vec![ 1, 5, 9],
vec![10, 11, 13],
vec![12, 13, 15]
], 9), 15);
assert_eq!(Solution::kth_smallest(vec![
vec![ 1],
], 1), 1);
}
}