《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 ...
随机推荐
- CRM WebClient UI和Hybris里工作中心跳转的url生成逻辑
CRM WebClient UI 把Work center的navigation target在client side不可见:在Chrome development tool里看不到,而是点击了Wor ...
- ArcGIS License Server Administrator 10.2 无法启动许可的解决办法
刚刚重装了电脑,安装ArcGIS的时候,安装完desktop之后又安装了License Manager,结果把破解文件替换完之后,发现ArcGIS License Server Administrat ...
- TCP与虚连接
http://bbs.csdn.net/topics/390262738 在TCP通信时,会建立一个从源端到目的端的虚拟连接.感觉这种连接类似电路交换,只是这种连接是虚拟存在的.发送的报文都应该是沿着 ...
- IOC、注入
转:https://blog.csdn.net/lutianfeiml/article/details/51731219 实际开发中使用XML还是注解 XML: bean管理 注解: 注入属性的时候比 ...
- 【转】Xcode真机调试初体验
1. 开发者证书(Certificates) 分为开发(iOS Development)和发布(iOS Distribution)两种,无论是真机调试,还是上传到App Store都需要该证书,是一个 ...
- Git版本控制的原理
Git工作区域 工作目录(Working Directory) 暂存区(Stage/Index) 资源库(Repository或Git Directory) 远程的git仓库(Remote Direc ...
- tarjan+topsort
题目 缩完点后统计入读为零的点就可以来. 因为缩完点后肯定是DAG #include<iostream> #include<cstdio> #include<algori ...
- 堆优化dijkstra
单源最短路径 题目链接:https://www.luogu.org/problemnew/show/P4779 直到做了这个题才发现我之前写的堆优化dijkstra一直是错的.. 这个堆优化其实很容易 ...
- barnes-hut算法 && Fast Multipole Methods算法
barnes-hut算法 http://arborjs.org/docs/barnes-hut Fast Multipole Methods算法 http://www.umiacs.umd.edu/~ ...
- 1.Netty入门
Netty入门 1.Netty介绍 (1)百度百科介绍: Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络 ...