Skip to content

Commit 1eb0c23

Browse files
committed
Create 1710-maximum-units-on-a-truck.rs
1 parent 42f0ff0 commit 1eb0c23

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

1710-maximum-units-on-a-truck.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// box_types.sort{|a, b|b[1]<=>a[1]}.map{|e|[e[1]]*e[0]}.flatten[...truck_size].sum
2+
3+
pub fn maximum_units(box_types: Vec<Vec<i32>>, truck_size: i32) -> i32 {
4+
let mut sorted_box_types = box_types.clone();
5+
sorted_box_types.sort_by(|a, b| b[1].cmp(&a[1]));
6+
let tmp = sorted_box_types.iter().map(|e|vec![e[1]; e[0] as usize]).collect::<Vec<Vec<i32>>>().into_iter().flatten().collect::<Vec<i32>>();
7+
let idx = vec![tmp.len(), truck_size as usize].into_iter().min().unwrap();
8+
tmp[..idx].iter().sum::<i32>()
9+
}
10+
11+
fn main() {
12+
let box_types = [[1,3],[5,5],[2,5],[4,2],[4,1],[3,1],[2,2],[1,3],[2,5],[3,2]].map(|e|e.to_vec()).to_vec();
13+
let truck_size = 35;
14+
println!("{:?}", maximum_units(box_types, truck_size));
15+
}

0 commit comments

Comments
 (0)