LeetCode 1105. Filling Bookcase Shelves

Problem Statement


use std::cmp;

impl Solution {
pub fn min_height_shelves(books: Vec<Vec<i32>>, shelf_width: i32) -> i32 {
let (n, mut i, mut j) = (books.len(), 0, 0);
let mut dp = vec![std::i32::MAX; n];
while i < n {
let (mut w, mut h) = (0, 0);
j = i;
while j < n {
w += books[j][0];
if w > shelf_width {
break;
}
h = cmp::max(h, books[j][1]);
let val = match i {
0 => 0,
_ => dp[i - 1]
};
dp[j] = cmp::min(dp[j], val + h);
j += 1;
}
i += 1;
}

return dp[n - 1];
}
}