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 ...
随机推荐
- 日常英语---十、MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击)
日常英语---十.MapleStory/Monsters/Level 191-201(Sad Erda:向你发射炮弹进行攻击) 一.总结 一句话总结:一种悲伤的精神,形成于奥术之河,一个黑法师能够融合 ...
- WPF打印涉及到的关键类
DocumentViewer--->FixedDocument----> PageContent ---->FixedPage
- Android中简单活动窗口的切换--Android
本例实现Android中简单Activity窗口切换:借助intent(意图)对应用操作(这里用按钮监听)等的描述,Android根据描述负责找对应的组件,完成组件的调用来实现活动的切换……案例比较简 ...
- LeetCode--278--第一个错误的版本
问题描述: 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个 ...
- webpack+vue多入口环境搭建
项目地址:https://pan.baidu.com/s/1c1Dflp2 使用前提:安装nodejs环境,webpack的配置官网的例子跟着跑一遍,会vue开发 研究webpack+vue研究了一个 ...
- js时间字符串转为标准时间
//将字符串转换为时间格式,适用各种浏览器,格式如2016-09-09T17:02:37.227"function GetTimeByTimeStr(dateString) { var ti ...
- test example
#coding=utf-8 import os import caffe import numpy as np root='/home/xxx/caffe/' #根目录 deploy=root + ' ...
- v-for
在实际的项目中,我们很多时候会碰到将JSON数据中的数组或对象渲染出列表之类的元素.在Vue中,提供了一个 v-for的指令,可以渲染列表. 组件和v-for 在自定义组件里,你可以像任何普通元素一样 ...
- [CodeForces - 197F] F - Opening Portals
F - Opening Portals Pavel plays a famous computer game. A player is responsible for a whole country ...
- Windows Visual Studio中修改PostgreSQL代码后调试报错
解决方法: 在源目录中重新install. D:\postgresql-9.6.0\src\tools\msvc>install E:\pg_install