叠罗汉II
叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上。为了使叠成的罗汉更稳固,我们应该让上面的人比下面的人更轻一点。现在一个马戏团要表演这个节目,为了视觉效果,我们还要求下面的人的身高比上面的人高。请编写一个算法,计算最多能叠多少人,注意这里所有演员都同时出现。
给定一个二维int的数组actors,每个元素有两个值,分别代表一个演员的身高和体重。同时给定演员总数n,请返回最多能叠的人数。保证总人数小于等于500。
测试样例:
[
[1,2],
[3,4],
[5,6],
[7,8]
],4
返回:4
解题
高的人,重的人在下面
矮的人,轻的人在上面
按照升高升序排序后,找到体重的最长上升子序列就好了
import java.util.*;
public class Stack {
public int getHeight(int[][] A, int n) {
// write code here
Arrays.sort(A, new myComparator());
int[] dp = new int[n];
int height = -1;
dp[0] = 1;
for(int i=1;i<n;i++){
int h = 0 ;
for(int j=0;j<i;j++){
if(A[i][1] > A[j][1]){
h = Math.max(h,dp[j]);
}
}
dp[i] = h + 1;
height = Math.max(dp[i],height);
}
return height;
}
class myComparator implements Comparator<int[]>{
public int compare(int[] a, int[] b) {
if(a[0] < b[0])
return -1;
else if(a[0] > b[0])
return 1;
else {
if(a[1] < b[1])
return -1;
else if(a[1] > b[1])
return 1;
else
return 0;
}
}
}
}
注意理解上面如何实现的排序
叠罗汉II的更多相关文章
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- 统计分析中Type I Error与Type II Error的区别
统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- [LeetCode] Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
随机推荐
- [转]安装openoffice,并且配置为windows服务
[转]安装openoffice,并且配置为windows服务 http://blog.csdn.net/zzzz3621/article/details/18400277 下载windows reso ...
- homework-05 大家一起玩游戏~
046 195 1.接口设计 客户端 用户登录后,启动一个线程来进行游戏,等待服务器信息及发送新数据 服务器 开始时,主程序一直等待用户登录,有新用户登录就开一个线程去为其服务 等到用户都登录完成 ...
- 通过find命令寻找文件并拷贝到一个指定目录方法详解
有这样的一个需求,需要将一部分符合条件的文件从一个目录拷贝到另一个目录中,可以通过使用find命令从源目录查找到符合条件的文件然后使用cp命令拷贝到目标目录 将通过find命令找到的文件拷贝到一个 ...
- python三级菜单
#-*- coding:utf-8 -*-#Author:gxli #一级菜单项def menu(): #遍历字典dic一级菜单 print('-----------一级菜单------------- ...
- linux 负载 待读
相关文章: 理解 Linux 的处理器负载均值(翻译)
- 如何将后台传来的json反序列化为前端具体对象
//jQuery方式 var obj = $.parseJSON(json); .... //eval var obj = eval("("+json+")" ...
- VS2013 IIS Express 添加MIME映射
VS2013,则可以直接在IIS Express中添加MIME映射.操作如下: 1.在DOS窗口下进入IIS Express安装目录,默认是“C:\Program Files\IIS Express” ...
- Windows准备Node.js运行与开发环境
如何在Windows环境下搭建Node.js开发环境:1.下载Node.js windows安装版http://www.nodejs.org/download/ 2.正常安装完成后,在系统环境变量已经 ...
- 腾讯qq的发展史
腾讯qq的发展史 即时通信软件的历史并不久远,但是它一诞生,就立即受到网民的喜爱,并风靡全球. 在其发展史上,以色列人功不可没.正是四位以色列籍的年轻人,在1996年7月成立的Mirabilis ...
- RobotFramework-关键字
地址:https://github.com/NitorCreations/RobotFramework-EclipseIDE/tree/master/plugin/robot-indices Coll ...