Cracking The Coding Interview 9.7
//原文:
//
// A circus is designing a tower routine consisting of people standing atop one another’s shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower.
//
//EXAMPLE:
//
//Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110)
//
//Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)
//
#include <iostream>
#include <algorithm>
using namespace std; struct people
{
public:
people(){};
people(int _h, int _w){h = _h; w = _w;};
int h;
int w;
}; bool compare(const people &p1, const people &p2)
{
if ( p1.h == p2.h)
{
return p1.w < p2.w;
}
return p1.h < p2.h;
} //求最长子序列,参考http://www.csie.ntnu.edu.tw/~u91029/LongestIncreasingSubsequence.html#1
int getLIS(people *p, int size)
{
int *length = new int[size];
for (int i = 0; i<size ; i++)
{
length[i] = 1;
} for (int i =0; i <size ; i++)
{
for (int j = i+1; j<size; j ++)
{
if (p[i].w < p[j].w)
{
length[j] = max(length[j], length[i] + 1);
}
}
} int n = length[0];
for (int i = 0; i < size; i++)
{
n = (length[i]>n? length[i]:n);
}
return n;
} int main()
{
people p[5] = {people(2,10), people(5,18), people(4,17), people(3,9), people(7,20)};
for (int i = 0; i<5; i++)
{
cout<<p[i].h<<" "<<p[i].w<<endl;
}
cout<<"============="<<endl;
sort(p,p+5,compare);
for (int i = 0; i<5; i++)
{
cout<<p[i].h<<" "<<p[i].w<<endl;
}
cout<<getLIS(p,5)<<endl;
return 0;
}
Cracking The Coding Interview 9.7的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 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>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- learn python the hard way习题31~40总结以及列表的扩展知识
Python 中的列表: 形式:[ 表示打开一个列表,中间的项目用 , 隔开,然后列表以 ] 结束. for循环 两种形式: for i in ArrayName: for i in range(0, ...
- 20165327 2017-2018-2 《Java程序设计》第4周学习总结
20165327 2017-2018-2 <Java程序设计>第4周学习总结 教材内容总结 第五章 继承是一种由已有的类创建新类的机制. class 子类名 extends 父类名{ - ...
- every day a practice —— morning(2)
Two years at sea have fostered a close relationship between the two fellow sailors as they cross the ...
- mat 使用 分析 oom 使用 Eclipse Memory Analyzer 进行堆转储文件分析
概述 对于大型 JAVA 应用程序来说,再精细的测试也难以堵住所有的漏洞,即便我们在测试阶段进行了大量卓有成效的工作,很多问题还是会在生产环境下暴露出来,并且很难在测试环境中进行重现.JVM 能够记录 ...
- 关于ActionBar 左侧添加完返回后 点击无效的问题
ActionBar actionBar =getSupportActionBar(); if(actionBar!=null){ actionBar.setHomeAsUpIndicator(R.mi ...
- JS实现继承的6种方式
使用pretotype,call实现完美继承 父类: fuction Animal(name){ this.name=name||"Animal"; this.sl ...
- win10系统同时安装python2和python3
1.官网下载python2和python3版本 2.安装python3,勾上Add Python3.5 to PATH,自定义选择安装目录,安装,验证:WIN+R--->cmd,输入python ...
- 百度基础架构组-实习生面试(2016.08 java后台开发)
一.项目 1.Spring MVC与Struts2的区别: 2.MVC三层是如何工作的?比如:要访问一个Url?a=xx&b=xx,怎么找到相应的资源,怎么运算,怎么返回等? 3.数据库myb ...
- PTA L2-002 链表去重
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 第一次做链表题,有时间多看看 解释 ...
- 2015-2016 ACM-ICPC Northeastern European Regional Contest (NEERC 15)C - Cactus Jubilee
题意:给一颗仙人掌,要求移动一条边,不能放在原处,移动之后还是一颗仙人掌的方案数(仙人掌:无向图,每条边只在一个环中),等价于先删除一条边,然后加一条边 题解:对于一颗仙人掌,分成两种边,1:环边:环 ...