《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 ...
随机推荐
- 笨办法学Python(二十九)
习题 29: 如果(if) 下面是你要写的作业,这段向你介绍了“if语句”.把这段输入进去,让它能正确执行.然后我们看看你是否有所收获. people = 20 cats = 30 dogs = 15 ...
- Js parsetInt() 字符串转换,只能转换字符串,数字开头的才会返回数值,否则为NaN,空字符串也返回NaN
alert(parseInt('456lee')); //456,返回正数部分 alert(parseInt('lee456lee')); //NaN alert(parseInt('lee456le ...
- 利用批处理结合Msbuild实现快速编译
我们经常在用vs2005做项目的时候会把一个项目分成几个模块(不管是对于功能上,还是系统构架上面),为的是以后部署,还有修改维护时候的方便.这样就会带来一个问题,随着模块的增加(这里所说得每个模块就是 ...
- mysql5.6之前需要账号的安全加固
mysql5.6之前需要账号的安全加固 从5.7开始就不需要了. delete from mysql.user where user!='root' or host='localhost'; flus ...
- 模拟插队,出队,POJ(2259)
题目链接:http://poj.org/problem?id=2259 水题一个,就是要记录一下每个队列是否有人bool[i], #include <iostream> #include ...
- 头部导航悬浮,css
.header{ position:fixed; z-index:100; left:; right:; } 如图.
- 1.Mysql集群------Docker下的Mysql主从复制
前言: 实话说,我想做的是Mysql集群架构. 我的计划是: 1.实现Docker下的Mysql主从复制 2.实现MyCat基于Mysql的读写分离 3.实现MyCat的分库分表 4.实现pxc集群 ...
- Mac openssl 和curl源码编译
1.先编译openssl, 下载源码后解压,终端进入源码目录,输入命令配置编译环境:./Configure darwin64-x86_64-cc 等待配置完成后,输入make 和make insta ...
- 交换机基础配置之单交换机划分vlan
我们以以上拓扑图为例 pc0的IP地址为:192.168.1.1 pc1的ip地址为:192.168.1.2 两台主机在同一网段,相互ping是能ping通的 我们的目的是在单交换机上划分两个vlan ...
- winrar压缩工具
WinRAR使用心得 免广告 英文版可以设置广告关闭,地址: https://www.win-rar.com/predownload.html?&Version=64bit 把WinRAR默认 ...