-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
42 lines (38 loc) · 961 Bytes
/
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
fn main() {
let mat = vec![
vec![1,1,0,0,0],
vec![1,1,1,1,0],
vec![1,0,0,0,0],
vec![1,1,0,0,0],
vec![1,1,1,1,1]
];
Solution::k_weakest_rows(mat, 3);
}
struct Solution {}
impl Solution {
pub fn k_weakest_rows(mat: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
let mut x: Vec<(i32, usize)> = mat.iter().enumerate().map(|(i, x)| (x.iter().sum(), i)).collect();
x.sort();
let mut x: Vec<i32> = x.into_iter().map(|(_, i)| i as i32).collect();
while x.len() > k as usize {
x.pop();
}
// println!("x = {:#?}", x);
x
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
let mat = vec![
vec![1,1,0,0,0],
vec![1,1,1,1,0],
vec![1,0,0,0,0],
vec![1,1,0,0,0],
vec![1,1,1,1,1]
];
assert_eq!(Solution::k_weakest_rows(mat, 3), vec![2,0,3]);
}
}