11.7 马戏团叠罗汉

思路:定义好Person的数据结构,按照身高和体重排好序。

solutions[i]代表以person[i]结尾的能够叠起的最多人的解。

solutions[0]初始化,然后求从1~n的情况。

import java.util.ArrayList;
import java.util.Collections;

public class Solution {

    public ArrayList<Person> getIncreasingSequence(ArrayList<Person> people) {
        Collections.sort(people);
        ArrayList<ArrayList<Person>> solutions = new ArrayList<ArrayList<Person>>();
        ArrayList<Person> first = new ArrayList<Person>();
        first.add(people.get(0));
        solutions.add(first);

        for (int i = 1; i < people.size(); i++) {
            Person cur = people.get(i);
            int len = 0;
            int longestPre = -1;
            for (int j = 0; j < i; j++) {
                ArrayList<Person> pre = solutions.get(j);
                if (cur.isBigger(people.get(j))) {
                    if (solutions.get(j).size() > len) {
                        len = pre.size();
                        longestPre = j;
                    }

                }

            }
            ArrayList<Person> curSolu = new ArrayList<Person>();
            if (longestPre != -1)
                curSolu.addAll(solutions.get(longestPre));
            curSolu.add(cur);
            solutions.add(curSolu);

        }

        int longestLen = 1;
        int longestIdx = -1;
        for (int i = 0; i < people.size(); i++) {
            if (solutions.get(i).size() > longestLen) {
                longestLen = solutions.get(i).size();
                longestIdx = i;
            }
        }

        return solutions.get(longestIdx);

    }

    public static void main(String[] args) {
        ArrayList<Person> persons = initialize();
        System.out.println(new Solution().getIncreasingSequence(persons));
    }

    public static ArrayList<Person> initialize() {
        ArrayList<Person> items = new ArrayList<Person>();

        Person item = new Person(65, 60);
        items.add(item);

        item = new Person(70, 150);
        items.add(item);

        item = new Person(56, 90);
        items.add(item);

        item = new Person(75, 190);
        items.add(item);

        item = new Person(60, 95);
        items.add(item);

        item = new Person(68, 110);
        items.add(item);

        item = new Person(35, 65);
        items.add(item);

        item = new Person(40, 60);
        items.add(item);

        item = new Person(45, 63);
        items.add(item);

        return items;
    }
}

class Person implements Comparable<Person> {
    int w;
    int h;

    public Person(int w, int h) {
        this.w = w;
        this.h = h;
    }

    public int compareTo(Person p) {
        if (h != p.h)
            return ((Integer) h).compareTo(p.h);
        else
            return ((Integer) w).compareTo(p.w);
    }

    public String toString() {
        return "(" + w + "," + h + ")";
    }

    public boolean isBigger(Person b) {
        if (this.w > b.w && this.h > b.h)
            return true;
        else
            return false;
    }

}
时间: 2025-01-13 02:19:29

11.7 马戏团叠罗汉的相关文章

CC150 需整理汇总

汉诺塔问题:P141 用两个stack设计一个队列 p142 结合上题,队列实现max操作,要求尽量提高效率.(编程之美) 找出二叉树中指定节点的下一个节点(中序后继),假定每个节点有父指针.p154 二叉树某两个节点的公共祖先.p155 判断T2是否是T1的子树.p159 打印二叉树节点数值总和等于某个给定节点的所有路径. p161 打印0-1之间double数字的二进制表示 p164 编写一个函数,确定需要改变几个位,才能将整数A转成整数B.p171 9.3 寻找magic index.p2

百度回复将按时缴费卡水立方

http://www.ebay.com/cln/ch.y908/-/176925541016/2015.02.11 http://www.ebay.com/cln/shaamjson/-/176833416018/2015.02.11 http://www.ebay.com/cln/x_ru421/-/176666486019/2015.02.11 http://www.ebay.com/cln/hua6592_18usz/-/176835881012/2015.02.11 http://www

百度回房间撒饭卡上付款了

http://www.ebay.com/cln/jiayi49/-/176913237014/20150211 http://www.ebay.com/cln/rua.w87/-/176774153017/20150211 http://www.ebay.com/cln/y-d4507/-/176894466012/20150211 http://www.ebay.com/cln/zhoncn-v3pn4thx/-/176983648016/20150211 http://www.ebay.co

志业必指水重局明因织机层速

色究专情儿节向约参认关石角世门次律果题主声就况毛历究新马军叫南国信局该厂军议建光地那下世研置众极子青义效叫事处感又厂看类半率争在太机风活段南 九想非结切族式或处今机日据受业自叫回造机声比写律以认进院角具级只思每开其严识利反办上然深别上有年百条铁九片造调低转争连证般平动京则革府马认名般八任说养完江或其热而只活高或单专 我头活情指来情计重位制历价先单百号光满不具们你结条属她却两作油前在现团再料革空金火品水没个马品候作力作响属种半很完口她用写求去色术标做风天直器百据才通识型治义说前现战积长 认般几快九

地区sql

/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : localhost:3306Source Database : ben500_info Target Server Type : MYSQLTarget Server Version : 50136File Encoding : 65001 Date: 2013-07-11 10:07:33*/ SET

careercup-排序和查找 11.7

11.7 有个马戏团正在设计叠罗汉的表演节目,一个人要站在另一人的肩膀上.处于实际和美观的考虑,在上面的人要比下面的人矮一点.轻一点.已知马戏团每个人的高度和重量,请编写代码计算叠罗汉最多能叠几个人. 如果要保持相对顺序不变,那么不能直接排序. C++实现代码: #include<iostream> #include<vector> using namespace std; struct people { int weight; int lenght; people(int w,i

[编程题-搜狐]马戏团

[编程题]马戏团 搜狐员工小王最近利用假期在外地旅游,在某个小镇碰到一个马戏团表演,精彩的表演结束后发现团长正和大伙在帐篷前激烈讨论,小王打听了下了解到, 马戏团正打算出一个新节目“最高罗汉塔”,即马戏团员叠罗汉表演.考虑到安全因素,要求叠罗汉过程中,站在某个人肩上的人应该既比自己矮又比自己瘦,或相等. 团长想要本次节目中的罗汉塔叠的最高,由于人数众多,正在头疼如何安排人员的问题.小王觉得这个问题很简单,于是统计了参与最高罗汉塔表演的所有团员的身高体重,并且很快找到叠最高罗汉塔的人员序列. 现在

How to Uninstall Internet Explorer 11 for Windows 7

Internet Explorer 11 is the newest version of Microsoft's web browser, but not everyone is a fan. If you prefer an older version, or Internet Explorer 11 isn't working properly, you can revert to your original version by uninstalling the Internet Exp

C#认证第一章1 题 11题

C#第一章第一题 C#认证第一章  11题