leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)
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)的更多相关文章
- [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 ...
- 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 ...
- 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题
Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...
- 【leetcode】354. Russian Doll Envelopes
题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...
- 354 Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [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 ...
- 动态规划——Russian Doll Envelopes
这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大 ...
随机推荐
- Linux使用者管理(2)---账号管理
用户添加 新增用户 sudo useradd -m username 这里必须使用sudo 因为需要对/etc/shadow进行读写,在ubuntu环境下,必须使用-m设置,否则不会创建主文件夹. 在 ...
- MyEclipse 2013 开发WebService
1.在Package Explorer窗口右键File新建WebService Project项目,我的名称为:TestWebService 2.WebService Framework选择JAX-W ...
- Django admin site(二)ModelAdmin methods
ModelAdmin methods save_model(request, obj, form, change) 此方法为admin界面用户保存model实例时的行为.request为HttpReq ...
- 单交换机VLAN虚拟局域网划分
1.下载Cisco模拟器 Packet Tracer 是由Cisco公司发布的一个辅助学习工具,为学习CCNA课程的网络初学者去设计.配置.排除网络故障提供了网络模拟环境.学生可在软件的图形用户界面上 ...
- MyEclipse Blue Edition 6.5 注册码生成程序
import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; im ...
- sed替换单引号
echo "Cfoo'barxml" | sed "s/'/::/g" | sed 's/::/\\:/g' | sed "s/:/'/g&quo ...
- 爬虫技术(五)-- 模拟简单浏览器(附c#代码)
由于最近在做毕业设计,需要用到一些简单的浏览器功能,于是学习了一下,顺便写篇博客~~大牛请勿喷,菜鸟练练手~ 实现界面如下:(简单朴素版@_@||) button_go实现如下: private vo ...
- poj 2828 Buy Tickets (线段树)
题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...
- 宏os_file_read_func
# define os_file_read(file, buf, offset, offset_high, n) \ os_file_read_func(file, buf, offset, offs ...
- uva111346Probability
求导. 大水题... 写这个题的目的就是要强调一些细节. printf输出%时要用2个%. 如果S>a*b的话,直接输出0,如果太小,直接输出100. 求导就不说了// 最关键的地方一笔带过?我 ...