-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1691: Maximum Height by Stacking Cuboids
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/problem_1691_maximum_height_by_stacking_cuboids/dynamic_programming.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Reverse; | ||
use std::convert::TryInto; | ||
use std::mem; | ||
|
||
impl Solution { | ||
pub fn max_height(cuboids: Vec<Vec<i32>>) -> i32 { | ||
let mut cuboids = cuboids | ||
.into_iter() | ||
.map(|cuboids| { | ||
let [width, length, height]: [_; 3] = cuboids.try_into().ok().unwrap(); | ||
let mut result = (width as u8, length as u8, height as u8, 0_u16); | ||
|
||
if result.1 < result.0 { | ||
mem::swap(&mut result.0, &mut result.1); | ||
} | ||
|
||
if result.2 < result.1 { | ||
mem::swap(&mut result.1, &mut result.2); | ||
} | ||
|
||
if result.1 < result.0 { | ||
mem::swap(&mut result.0, &mut result.1); | ||
} | ||
|
||
result | ||
}) | ||
.collect::<Box<_>>(); | ||
|
||
cuboids.sort_unstable_by_key(|&(x, y, z, _)| Reverse((x, y, z))); | ||
|
||
let mut result = 0; | ||
|
||
for i in 0..cuboids.len() { | ||
let (current, left_cache) = cuboids[..=i].split_last_mut().unwrap(); | ||
let mut max_height = 0; | ||
|
||
for left in left_cache { | ||
if current.1 <= left.1 && current.2 <= left.2 { | ||
max_height = max_height.max(left.3); | ||
} | ||
} | ||
|
||
current.3 = max_height + u16::from(current.2); | ||
result = result.max(current.3); | ||
} | ||
|
||
i32::from(result) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_height(cuboids: Vec<Vec<i32>>) -> i32 { | ||
Self::max_height(cuboids) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/problem_1691_maximum_height_by_stacking_cuboids/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
pub mod dynamic_programming; | ||
|
||
pub trait Solution { | ||
fn max_height(cuboids: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[50, 45, 20], [95, 37, 53], [45, 23, 12]] as &[_], 190), | ||
(&[[38, 25, 45], [76, 35, 3]], 76), | ||
( | ||
&[ | ||
[7, 11, 17], | ||
[7, 17, 11], | ||
[11, 7, 17], | ||
[11, 17, 7], | ||
[17, 7, 11], | ||
[17, 11, 7], | ||
], | ||
102, | ||
), | ||
( | ||
&[ | ||
[92, 47, 83], | ||
[75, 20, 87], | ||
[68, 12, 83], | ||
[12, 85, 15], | ||
[16, 24, 47], | ||
[69, 65, 35], | ||
[96, 56, 93], | ||
[89, 93, 11], | ||
[86, 20, 41], | ||
[69, 77, 12], | ||
[83, 80, 97], | ||
[90, 22, 36], | ||
], | ||
447, | ||
), | ||
( | ||
&[ | ||
[74, 66, 55], | ||
[97, 97, 38], | ||
[21, 69, 43], | ||
[88, 83, 87], | ||
[11, 4, 96], | ||
[17, 98, 21], | ||
[45, 43, 12], | ||
[19, 67, 24], | ||
[72, 5, 81], | ||
[30, 53, 100], | ||
[38, 30, 29], | ||
[81, 53, 42], | ||
[78, 80, 9], | ||
[3, 80, 66], | ||
[74, 16, 92], | ||
[18, 17, 70], | ||
[66, 88, 56], | ||
[7, 51, 49], | ||
[9, 59, 13], | ||
[44, 67, 21], | ||
[9, 95, 14], | ||
[88, 100, 37], | ||
[23, 76, 24], | ||
[15, 38, 41], | ||
[47, 98, 66], | ||
[25, 39, 23], | ||
[74, 49, 28], | ||
[100, 82, 62], | ||
[96, 73, 52], | ||
[9, 22, 5], | ||
[83, 99, 28], | ||
[9, 35, 5], | ||
[26, 53, 33], | ||
[53, 98, 93], | ||
], | ||
605, | ||
), | ||
]; | ||
|
||
for (cuboids, expected) in test_cases { | ||
assert_eq!( | ||
S::max_height(cuboids.iter().copied().map(Vec::from).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |