https://leetcode.com/problems/russian-doll-envelopes/

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

class pair {
public int width;
public int height; public pair(int w, int h) {
super();
this.width = w;
this.height = h;
}
} class pairComparator implements Comparator {
public int compare(Object o1, Object o2) {
pair p1 = (pair) o1;
pair p2 = (pair) o2; if(p1.width < p2.width) { return -1; } else if(p1.width == p2.width) { if(p1.height == p2.height) {
return 0;
} else if(p1.height < p2.height) {
return -1;
} else {
return 1;
} } else { return 1;
}
}
} public class Solution {
public int maxEnvelopes(int[][] envelopes) { int n = envelopes.length;
if(n == 0) {
return 0;
} pair pr[] = new pair[n];
for(int i=0; i<n; ++i) {
pair p = new pair(envelopes[i][0], envelopes[i][1]);
pr[i] = p;
} Arrays.sort(pr, new pairComparator()); int[] dp = new int[n];
int rs = -1;
for(int i=0; i<n; ++i) {
int mmax = 0;
for(int pre=0; pre<i; ++pre) {
if(pr[pre].width < pr[i].width && pr[pre].height < pr[i].height) {
mmax = Math.max(mmax, dp[pre]);
}
}
dp[i] = mmax + 1;
rs = Math.max(rs, dp[i]);
} return rs;
}
}

leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)的更多相关文章

  1. [LeetCode] 354. Russian Doll Envelopes 俄罗斯套娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  2. leetCode 354. Russian Doll Envelopes

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  3. 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

    Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...

  4. 【leetcode】354. Russian Doll Envelopes

    题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...

  5. 354 Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  6. 354. Russian Doll Envelopes

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  7. [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  8. [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  9. 动态规划——Russian Doll Envelopes

    这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大 ...

随机推荐

  1. 8、SpringMVC源码分析(3):分析ModelAndView的形成过程

    首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...

  2. AjaxUpLoad.js使用实现文件上传

    AjaxUpLoad.js的使用实现无刷新文件上传,如图. 图1 文件上传前 图2 文件上传后 1.创建页面并编写HTML [html] view plaincopy   上传文档: <div  ...

  3. [Lintcode 3sum]三数之和(python,二分)

    题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...

  4. Python模块整理(三):子进程模块subprocess

    文章 原始出处 http://ipseek.blog.51cto.com/1041109/807513. 本来收集整理网络上相关资料后整理: 从python2.4版本开始,可以用subprocess这 ...

  5. HDU 1358 (所有前缀中的周期串) Period

    题意: 给出一个字符串,在所有长度大于1的前缀中,求所有的周期至少为2的周期串,并输出一个周期的长度以及周期的次数. 分析: 有了上一题 HDU 3746 的铺垫,这道题就很容易解决了 把next求出 ...

  6. textContent、innerText的用法,在文档中插入纯文本

    有时候需要查询纯文本形式的元素内容,或者在文档中插入纯文本.标准的方法是用Node的textContent属性来实现: var para = document.getElementsByTagName ...

  7. Android基础_1 四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service(服务),Content Provider(内容提供者),BroadcastReceiver(广播接收器). 一.四大基本组件 Acti ...

  8. 【转】eclipse中egit插件使用

    原文网址:http://my.oschina.net/songxinqiang/blog/192567 eclipse和git这个两个工具的使用人数都是相当多的,在eclipse里面也有egit插件来 ...

  9. andorid 下拉刷新

    1:android的下拉刷新操作是需要一个ListView,通过onTouchEvent来判断用户的手势操作,用户触摸屏幕并且下拉时,当下拉超过指定的设定高度时就提示用户进行刷新. 2:当进行刷新时, ...

  10. 如何在linux中搭建JEECMS系统

    本人正在进行jeecms二次开发,但因win7系统中的Tomcat无法使用,就想起在linux下安装,但去jeecms的官方网站,没有给出在linux下安装的方法,确实苦恼,经过一天的研究,终于大功告 ...