9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Implement a method to build the tallest stack possible, where the height of a stack is the sum of the heights of each box.

开始看到这题时,以为是3.4 Towers of Hanoi 汉诺塔,其实不太一样,这道题只是单纯的让我们垒箱子而已,大的在最底下,问我们能垒出的最大高度是多少。也是一道用递归来解的题,首先我们要先实现箱子类Box,里面包含了箱子的尺寸宽,高和深度,然后还要有一个成员函数canBeAbove,用来判断当前箱子能否放到另一个箱子的上面,还有一个静态函数,是求一摞箱子的总高度的。然后我们对每个箱子都调用一次递归,然后维护一个最大值,每次递归都更新这个最大值,那么最终递归结束后这个最大值就是所求,参见代码如下:

解法一:

class Box {
public:
int _width, _depth, _height;
Box(int w, int d, int h): _width(w), _depth(d), _height(h) {}
bool canBeAbove(Box *bottom) {
if (bottom == nullptr) return true;
return _width > bottom->_width && _depth > bottom->_depth && _height > bottom->_height;
}
static int stackHeight(vector<Box*> stack) {
int res = ;
for (auto &a : stack) {
res += a->_height;
}
return res;
}
}; class Solution {
public:
vector<Box*> createStack(vector<Box*> boxes) {
return createStack(boxes, nullptr);
}
vector<Box*> createStack(vector<Box*> boxes, Box *bottom) {
vector<Box*> res;
int max_height = ;
for (auto &a : boxes) {
if (a->canBeAbove(bottom)) {
vector<Box*> new_stack = createStack(boxes, a);
int new_height = Box::stackHeight(new_stack);
if (new_height > max_height) {
res = new_stack;
max_height = new_height;
}
}
}
if (bottom != nullptr) res.push_back(bottom);
return res;
}
};

上述代码虽然正确,但是不高效,像之前那道9.8 Represent N Cents 美分的组成一样,我们也可以用哈希表来优化,保存我们之前算过的最优解,那么在递归调用需要相同的结果时,就可以直接从哈希表中调用,参见代码如下:

解法二:

class Solution {
public:
vector<Box*> createStack(vector<Box*> boxes) {
unordered_map<Box*, vector<Box*> > m;
return createStack(boxes, nullptr, m);
}
vector<Box*> createStack(vector<Box*> &boxes, Box *bottom, unordered_map<Box*, vector<Box*> > &m) {
if (bottom != nullptr && m.find(bottom) != m.end()) {
return m[bottom];
}
vector<Box*> res;
int max_height = ;
for (auto &a : boxes) {
if (a->canBeAbove(bottom)) {
vector<Box*> new_stack = createStack(boxes, a, m);
int new_height = Box::stackHeight(new_stack);
if (new_height > max_height) {
res = new_stack;
max_height = new_height;
}
}
}
if (bottom != nullptr) {
res.push_back(bottom);
m[bottom] = res;
}
return res;
}
};

[CareerCup] 9.10 Stack Boxes 垒箱子问题的更多相关文章

  1. [CareerCup] 18.10 Word Transform 单词转换

    18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...

  2. [CareerCup] 17.10 Encode XML 编码XML

    17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a ...

  3. [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表

    8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...

  4. [CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组

    13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the n ...

  5. 给jdk写注释系列之jdk1.6容器(10)-Stack&Vector源码解析

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈.      什么是栈呢,我就不找它具体的定义了,直接举个例子,栈就相当于一个很窄的木桶 ...

  6. UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索

    题意:给出几个多维的箱子,如果箱子的每一边都小于另一个箱子的对应边,那就称这个箱子小于另一个箱子,然后要求能够套出的最多的箱子. 要注意的是关系图的构建,对箱子的边排序,如果分别都小于另一个箱子就说明 ...

  7. poj 1475 Pushing Boxes 推箱子(双bfs)

    题目链接:http://poj.org/problem?id=1475 一组测试数据: 7 3 ### .T. .S. #B# ... ... ... 结果: //解题思路:先判断盒子的四周是不是有空 ...

  8. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  9. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. 设计模式 --- 模型-视图-控制器(Model View Controller)

    模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...

  2. 《HTML5与CSS3实例教程》

    <HTML5与CSS3实例教程> 基本信息 作者: (美)Brian P. Hogan 译者: 卢俊祥 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:97871153634 ...

  3. IIS管理

    1.缓存的处理 http://www.cnblogs.com/dudu/p/iis_user-mode_caching_cache-control_public.html 2.负载均衡的使用 ARR ...

  4. Linux 下编译安装软件,找不到共享库 xx.so 的解决办法

    编译memcached时,报错没有libevent,于是下载libevent,configure , make && make install ,然后在重新安装memcache成功之后 ...

  5. hibernate查询方式

    hibernate查询方式:1.本地SQL查询 2.HQL查询 3.QBC查询 HQL查询:是面向对象的查询语言,是使用最广的一种查询方法 QBC查询:Query by Criteria是一套接口来实 ...

  6. 利用API方式进行数据库的增删改查

    /* 将数据库的增删改查单独放进一个包 */ package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java ...

  7. MySQL中EXPLAIN的解释

    EXPLAIN是查看MySQL优化器如何决定执行查询的主要方法,这个功能具有局限性,以为它并总是会说出真相,但是却可以获得最好信息. 学会解释EXPLAIN,你就会了解MySQL优化器是如何工作,你才 ...

  8. Java之美[从菜鸟到高手演变]之JVM内存管理及垃圾回收

    很多Java面试的时候,都会问到有关Java垃圾回收的问题,提到垃圾回收肯定要涉及到JVM内存管理机制,Java语言的执行效率一直被C.C++程序员所嘲笑,其实,事实就是这样,Java在执行效率方面确 ...

  9. openfire+asmack搭建的安卓即时通讯(二) 15.4.9

    上期没有放成果图呢!忘了=-=,这就是上次的成果图,textview里面会显示登陆的名字(这个是默认管理员帐号=-=) 好吧,登陆了服务器我们就有了交互的功能啦可以说是前进了一大步呢!下面能我们就要试 ...

  10. 【转载】PMC/PEC Boundary Conditions and Plane Wave Simulation

    原文链接 PMC/PEC Boundary Conditions and Plane Wave Simulation (FDTD) OptiFDTD now has options to use Pe ...