Skip to content

Commit

Permalink
Add problem 1691: Maximum Height by Stacking Cuboids
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 14, 2023
1 parent 8fa4a0d commit 36214ff
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ pub mod problem_1686_stone_game_vi;
pub mod problem_1688_count_of_matches_in_tournament;
pub mod problem_1689_partitioning_into_minimum_number_of_deci_binary_numbers;
pub mod problem_1690_stone_game_vii;
pub mod problem_1691_maximum_height_by_stacking_cuboids;
pub mod problem_1694_reformat_phone_number;
pub mod problem_1695_maximum_erasure_value;
pub mod problem_1696_jump_game_vi;
Expand Down
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 src/problem_1691_maximum_height_by_stacking_cuboids/mod.rs
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,
);
}
}
}

0 comments on commit 36214ff

Please sign in to comment.