📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 4825| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : 马戏团人塔问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
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)

算简单的吧

上一篇:Amazon : find all primes less than n
下一篇:Microsoft : find anagram pairs
无效楼层,该帖已经被删除
🔗
 楼主| wwwyhx 2011-6-13 15:08:07 | 只看该作者
全局:
本帖最后由 wwwyhx 于 2011-6-13 20:32 编辑
Similar to longest increasing subsequence. Sort element. Then for each person at index i, find the person whose height and weight is smaller but with largest score s. Then the person at index i score s+1; track each person's previous person's idx for final printing.

        public static void maxLength(List a){
        // using dynamic programming.
                // similar to longest increasing subsequence.
               
                // sort array first, takes O(nlogn)
                Collections.sort(a);
               
                // use an array to track the idx of  ...
hkgzyx 发表于 2011-6-13 07:36



    我没仔细看代码,你测试过觉得可行吗?或者再详细说说算法??
    不是很懂JAVA,这个Collections.sort 是根据什么来排序的?
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-13 20:33:54 | 只看该作者
全局:
给出个我的解法:
1. 对每个人的高度排序,为A列
2. 对每个人的体重排序,为B列
3. 找出A,B的最大公共子序列,为C,

C即为答案
回复

使用道具 举报

🔗
lambda2fei 2012-9-4 17:13:15 | 只看该作者
全局:
wwwyhx 发表于 2011-6-13 20:33
给出个我的解法:
1. 对每个人的高度排序,为A列
2. 对每个人的体重排序,为B列

最长上升子序列,对高度排序然后对排序后的数组X找出按体重的最长上升子序列即可。时间复杂度O(nlogn)。
楼主这个解法也是正确的,可以证明AB的任一公共子序列都是X的上升子序列,且X的任一上升子序列都是AB的公共子序列。但是这个解法需要O(n^2)。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表