《Cracking the Coding Interview》——第11章:排序和搜索——题目7
2014-03-21 22:05
题目:给你N个盒子堆成一座塔,要求下面盒子的长和宽都要严格大于上面的。问最多能堆多少个盒子?
解法1:O(n^2)的动态规划解决。其实是最长递增子序列问题,所以也可以用O(n * log(n))的优化算法。
代码:
// 11.7 n boxes are to stack up to a tower. Every box must be strictly smaller in width and height than the one right below it.
// How many boxes at most can you stack up?
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
int x;
int y;
Box(int _x = , int _y = ): x(_x), y(_y) {}; bool operator < (const Box &other) {
if (x != other.x) {
return x < other.x;
} else {
return y < other.y;
}
};
}; int main()
{
vector<Box> v;
vector<int> dp;
int n;
int i, j;
int res; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d", &v[i].x, &v[i].y);
}
sort(v.begin(), v.end());
dp.resize(n);
res = ;
for (i = ; i < n; ++i) {
dp[i] = ;
for (j = ; j < i; ++j) {
if (v[j].x < v[i].x && v[j].y < v[i].y) {
dp[i] = dp[j] + > dp[i] ? dp[j] + : dp[i];
}
}
res = dp[i] > res ? dp[i] : res;
}
printf("%d\n", res); v.clear();
dp.clear();
} return ;
}
解法2:用二分查找优化后的代码,其中使用了STL算法库提供的lower_bound(),二分也不总是要手写的。
代码:
// 11.7 n boxes are to stack up to a tower. Every box must be strictly smaller in width and height than the one right below it.
// How many boxes at most can you stack up?
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std; struct Box {
int x;
int y;
Box(int _x = , int _y = ): x(_x), y(_y) {}; bool operator < (const Box &other) {
if (x != other.x) {
return x < other.x;
} else {
return y < other.y;
}
};
}; int main()
{
vector<Box> v;
vector<int> dp;
vector<int>::iterator it;
int n;
int i; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d%d", &v[i].x, &v[i].y);
}
sort(v.begin(), v.end());
dp.push_back(v[].y);
for (i = ; i < n; ++i) {
if (v[i].y > dp[dp.size() - ]) {
dp.push_back(v[i].y);
} else {
it = lower_bound(dp.begin(), dp.end(), v[i].y);
*it = v[i].y;
}
}
printf("%d\n", (int)dp.size()); v.clear();
dp.clear();
} return ;
}
《Cracking the Coding Interview》——第11章:排序和搜索——题目7的更多相关文章
- 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目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 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(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目3
2014-03-21 20:55 题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位.找出其中是否存在某一个值. 解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目2
2014-03-21 20:49 题目:设计一种排序算法,使得anagram排在一起. 解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数. 代码: // 11.2 ...
随机推荐
- 关于定义顺序和内存分配的关系--记一道不严谨的C语言题
include<stdio.h> #include<iostream> int main() { char a[] = "123"; char b[] = ...
- Angular2入门--架构概览
Angular 介绍 Angular 是一款来自谷歌的开源的web前端框架,诞生于2009年,是一款优秀的前端JS框架,已经被用于谷歌的多款产品. Angular 基于Typescript开发 ,更适 ...
- 2017.11.23 利用Cookie管理实现自动登陆
Cookie管理 Cookie对象是由服务器产生并保存在客户端的信息,常用他记录用户个人信息以及个性化设置.用户每次访问网点时,应用程序就可以检索以前保存的信息 Cookie对象属于的类是javax. ...
- php开启短标签支持
打开php.ini,找到 short_open_tag = Off ,将 Off 改为 On
- iframe的Dom操作
我最近遇到这样一个需求, 抛开业务相关不谈,但从技术上说:页面中选择公司中的页面,在iframe中展示被选的页面,并且要对页面做一些Dom相关的处理.也就是说我们需要在父级页面中操作子页面(ifram ...
- Nginx配置文件详细解释
转自:https://www.cnblogs.com/knowledgesea/p/5175711.html 序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开 ...
- mybatis-generator的maven插件使用异常(mybatis-generator-maven-plugin):generate failed: Exception getting JDBC Driver
使用mybatis的代码生成工具:mybatis-generator,在父model中引入了maven插件的依赖,如下: <!-- Mybatis.generator插件 --> < ...
- 1.初识Quartz
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/QuartzDemo.git 前言: 接触一个新事物的开始,我们都会产生一些疑问: Quartz是什 ...
- 《C++总结3》
派生类 Class student1:public student //表示公用继承,默认为私有的 { public : …… …… } 继承的时候一定是全部继承来,但是可以自己设定访问属性,或 ...
- Python 初始—(高阶函数)
变量可以指向函数,函数的参数能接收变量, 将函数通过参数进行传递 def SetAbs(a,b,abs){ return abs(a)+abs(b) }