1105. 填充书架
题目链接:1105. 填充书架
方法一:记忆化搜索
解题思路
:从 到 书放置的最小高度总和;- 对于每一层:枚举当前层放置从
开始的书,放置几本时整体的高度最优,按题目要求,必须是从 开始的连续几本书,当前层的高度取最优方案中书的最高值; - 返回
。
代码
class Solution {
public:
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
const int n = books.size();
int cache[n]; memset(cache, -1, sizeof(cache));
function<int(int)> dfs = [&](int i) -> int {
if (i == n) return 0;
if (cache[i] != -1) return cache[i];
int &res = cache[i], width = shelfWidth, high = 0;
res = INT_MAX;
for (int j = i; j < n; j ++ ) { // 枚举
if (width < books[j][0]) break;
width -= books[j][0];
high = max(high, books[j][1]);
res = min(res, dfs(j + 1) + high); // 递归到下一层,取所有方案的最小值
}
return res;
};
return dfs(0);
}
};
复杂度分析
时间复杂度:
空间复杂度:
方法二:动态规划
解题思路
将记忆化 => 动态规划
代码
class Solution {
public:
int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
int n = books.size();
int dp[n + 1]; dp[n] = 0;
for (int i = n - 1; i >= 0; i -- ) {
int width = shelfWidth, high = 0;
dp[i] = INT_MAX;
for (int j = i; j < n; j ++ ) {
if (width < books[j][0]) break;
width -= books[j][0];
high = max(high, books[j][1]);
dp[i] = min(dp[i], dp[j + 1] + high); // 求当前层为 [i, j],先前所有层为 [j + 1, n - 1] 为最优方案的 j
}
}
return dp[0];
}
};
复杂度分析
时间复杂度:
空间复杂度:
分类:
leetcode题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】