1105.填充书架
链接:1105.填充书架
难度:Medium
标签:数组、动态规划
简介:给定一个数组 books ,其中 books[i] = [thicknessi, heighti] 表示第 i 本书的厚度和高度。你也会得到一个整数 shelfWidth 。每一层所摆放的书的最大高度就是这一层书架的层高,书架整体的高度为各层高之和。以这种方式布置书架,返回书架整体可能的最小高度。
题解 1 - cpp
- 编辑时间:2023-04-23
- 执行用时:4ms
- 内存消耗:7.9MB
- 编程语言:cpp
- 解法介绍:dp[i]表示以i为行末的最大高度。
class Solution {
public:
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
int n = books.size();
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
int sum = 0, h = 0;
for (int j = i - 1; j >= 0; j--) {
if (sum + books[j][0] > shelfWidth) break;
sum += books[j][0];
h = max(h, books[j][1]);
dp[i] = min(dp[i], dp[j] + h);
}
}
return dp[n];
}
};
题解 2 - python
- 编辑时间:2023-04-23
- 执行用时:40ms
- 内存消耗:15.3MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
def minHeightShelves(self, books: List[List[int]], shelfWidth: int) -> int:
n = len(books)
dp = [inf] * (n + 1)
dp[0] = 0
for i in range(1, n+1):
sum = h = 0
for j in range(i-1, -1, -1):
if sum + books[j][0] > shelfWidth:
break
sum += books[j][0]
h = max(h, books[j][1])
dp[i] = min(dp[i], dp[j]+h)
return dp[n]
题解 3 - rust
- 编辑时间:2023-04-23
- 内存消耗:2.3MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
pub fn min_height_shelves(books: Vec<Vec<i32>>, shelf_width: i32) -> i32 {
use std::cmp::{max, min};
let n = books.len();
let mut dp = vec![i32::MAX; n + 1];
dp[0] = 0;
for i in 1..=n {
let mut sum = 0;
let mut h = 0;
for j in (0..=i - 1).rev() {
if sum + books[j][0] > shelf_width {
break;
}
sum += books[j][0];
h = max(h, books[j][1]);
dp[i] = min(dp[i], dp[j] + h);
}
}
dp[n]
}
}