叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上。为了使叠成的罗汉更稳固,我们应该让上面的人比下面的人更轻一点。现在一个马戏团要表演这个节目,为了视觉效果,我们还要求下面的人的身高比上面的人高。请编写一个算法,计算最多能叠多少人,注意这里所有演员都同时出现。

给定一个二维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的更多相关文章

  1. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  2. 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 ...

  3. 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 ...

  4. 函数式Android编程(II):Kotlin语言的集合操作

    原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...

  5. 统计分析中Type I Error与Type II Error的区别

    统计分析中Type I Error与Type II Error的区别 在统计分析中,经常提到Type I Error和Type II Error.他们的基本概念是什么?有什么区别? 下面的表格显示 b ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. [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 ...

  8. [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 ...

  9. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

随机推荐

  1. html readme

    取html页面高度 document.documentElement.scrollHeight在IE和Chrome下,可以正常取到合适的全文高度,但是firefox下取到的则过高: 用document ...

  2. responsive menu

    http://responsive-nav.com/#instructions https://github.com/viljamis/responsive-nav.js http://tympanu ...

  3. algorithm之不变序列操作

    概述:不变序列算法,参见http://www.cplusplus.com/reference/algorithm/ /* std::for_each template <class InputI ...

  4. 20145120 《Java程序设计》第8周学习总结

    20145120 <Java程序设计>第8周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点 read()将ReadableByteChannel中的数据读至By ...

  5. android开发类似coverflow效果的3d旋转

    源码下载地址:http://download.csdn.net/detail/feijian_/8888219

  6. EntityFramework走马观花之CRUD(上)

    对于任何一个ORM框架,CRUD都是其核心功能,可以这么说,CRUD功能实现得好坏,直接决定了此ORM框架的命运. CRUD是英文Create.Read.Update.Delete四个单词的缩写,对应 ...

  7. Hibernate映射类型对照表

    Hibernate映射类型对照表 java类型  Hibernate映射类型  SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...

  8. 资料共享平台----nabcd

    知识共享平台NABCD模型 N(need)需求 大一新生刚刚开始大学生活,不适应大学学习生活的节奏,并且课堂上知识容量大.密度高,学生不能立刻掌握所学知识点,同时,网上资料冗杂繁复,指向性不强,导致学 ...

  9. 开发EXTMVC框架前需要了解的基础知识整理

    1.组件选择器 目的:了解如何选择Extjs中的组件,就跟学习jquery时一定会先要学习:$()选择器一样. 常用场景:       1.在controller中的control事件中用到      ...

  10. bzoj 1270 DP

    w[i,j]代表高度j,第i颗树的时候的最大值 那么w[i,j]:=max(w[i,j+1],w[k,j+heigh])+sum[i,j]: 但是这样枚举是n^3的,我们发现转移的第二个选择w[k,j ...