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

给定一个二维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. 基于HTML5的可预览多图片Ajax上传

    一.关于图片上传什么什么的 在XHTML的时代,我们使用HTML file控件上传图片一次只能上传一张.要一次上传多图,做法是借助于flash.例如swfupload.js.可惜,使用复杂的点,比如f ...

  2. [转]ubuntu 下minicom超级终端的使用方法

    [转]ubuntu 下minicom超级终端的使用方法 http://blog.chinaunix.net/uid-25909619-id-3184639.html 系统环境: Ubuntu 11.0 ...

  3. What is the difference between position: static,relative,absolute,fixed

    What is the difference between static,relative, absolute,fixed we can refer to this link: expand

  4. 61.MII、RMII、GMII接口的详细介绍

    概述: MII (Media Independent Interface(介质无关接口)或称为媒体独立接口,它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口和一个MAC和PHY之间的管 ...

  5. UIScrowView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  6. JS中showModalDialog 详细使用

    基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...

  7. 一个flag

    最近要学的东西 1.矩阵树定理 2.KM 3.FFT 4.单纯型 5.自动机系列 6.插头DP 7.计算几何(?) 8.数学相关(?)

  8. Understanding Service Types

    Last update: June 2014. I have partially rewritten this article to provide more technical details an ...

  9. ATR与ATS

    ATR:answer to reset  复位应答 ATS:answer to select 选择应答

  10. 团队开发(NABC)

    特点:这是一个手机软件,能通过通讯录录入生日信息 N(Need需求):现在在交际圈中需要记住越来越多朋友的生日信息 A(Approach做法):由一个简单的闹钟为基础,添加与生日相关的功能,最终实现 ...