《Cracking the Coding Interview》——第9章:递归和动态规划——题目10
2014-03-20 04:15
题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的。如果你不能旋转盒子变换长宽高,这座塔最高能堆多高?
解法:首先将n个盒子按照长宽高顺序排好序,然后动态规划,我写了个O(n^2)时间复杂度的代码。
代码:
// 9.10 A stack of n boxes is form a tower. where every stack must be strictly larger than the one right above it.
// The boxes cannot be rotated.
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
// width
int w;
// height
int h;
// depth
int d;
Box(int _w = , int _h = , int _d = ): w(_w), h(_h), d(_d) {}; bool operator < (const Box &other) {
if (w != other.w) {
return w < other.w;
} else if (h != other.h) {
return h < other.h;
} else {
return d < other.d;
}
};
}; int main()
{
int n, i, j;
Box box;
vector<Box> v;
vector<int> dp;
int result; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d%d", &v[i].w, &v[i].h, &v[i].d);
}
sort(v.begin(), v.end());
dp.resize(n);
for (i = ; i < n; ++i) {
dp[i] = v[i].h;
}
for (i = ; i < n; ++i) {
for (j = ; j < i; ++j) {
if (v[i].w > v[j].w && v[i].h > v[j].h && v[i].d > v[j].d) {
dp[i] = v[i].h + dp[j] > dp[i] ? v[i].h + dp[j] : dp[i];
}
}
}
result = dp[];
for (i = ; i < n; ++i) {
result = dp[i] > result ? dp[i] : result;
}
v.clear();
dp.clear();
printf("%d\n", result);
} return ;
}
《Cracking the Coding Interview》——第9章:递归和动态规划——题目10的更多相关文章
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9
2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...
随机推荐
- Selenium入门19 捕获异常
脚本出现异常时会中断执行,想要继续执行就要做异常处理: 1 try ... except .... else 遇到异常显示异常信息: 没有异常继续执行else后面的脚本 2 try ... exc ...
- ASP .NET CORE 读取配置文件的方法
老式的config文件在ASP.net core2.0中变成了appsettings.json,如果想要读取自定义配置,可以写如下代码 { "Logging": { "I ...
- eclipse的一些快捷键
ctrl + 1快速修复 ctrl + d 快速删除 ctrl + F11快速运行 ctrl + m 放大工作区 atl + /注释 ...
- Python中的__name__和__main__含义详解
1背景 在写Python代码和看Python代码时,我们常常可以看到这样的代码: ? 1 2 3 4 5 def main(): ...... if __name == "__m ...
- ajax(form)图片上传(spring)
第一步:spring-web.xml <!--配置上传下载--> <bean id="multipartResolver" class="org.spr ...
- rectified units
from: http://en.wikipedia.org/wiki/Rectifier_(neural_networks) In the context of artificial neural n ...
- rbg的代码
不得不赞rbg的代码,写的是真的好,各种异常都考虑到了,至少常见的异常没有了. 还有selective search的代码,也是很赞. 而edgebox的代码则不行啊,demo写的太死,而且代码里只能 ...
- td过长,将固定宽度table撑开
解决办法: 在table上加上样式: table{table-layout:fixed;word-break:break-all} table-layout:fixed tablle的列宽由表格宽 ...
- jquery 操作css 尺寸
.height() 获取元素集合中的第一个元素的当前计算高度值,或设置每一个匹配元素的高度值. .height() 获取匹配元素集合中的第一个元素的当前计算高度值. 这个方法不接受参数. $(wind ...
- java乱码问题
我们知道JSP页面是需要转换为servlet的,在转换过程中肯定是要进行编码的.在JSP转换为servlet过程中下面一段代码起到至关重要的作用. <%@ page language=" ...