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信封,现在给定一组信封的大 ...
随机推荐
- OpenCV源码阅读(1)---matx.h---mat类与vec类
matx.h matx类是opencv中的一个基础类,其位于core模块中,所执行的操作时opencv矩阵和向量的运算.如果熟悉基于matlab的图像处理,那么很容易想到,所有对图像的操作归根结底都是 ...
- 【c/c++】内存分配大小
测试平台:linux 32位系统 用sizeof()运算符计算分配空间大小.单位:字节 1. 数组名与变量名的区别 int main() { char q[] = "hello"; ...
- Django admin的一些有用定制
Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...
- lightOJ 1326 Race(第二类Stirling数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1326 题意:有n匹马赛跑.问有多少种不同的排名结果.可以有多匹马的排名相同. 思路:排 ...
- Android app Splash页的替代方案
一般的App想要显示公司的log什么的,都会在启动的第一个页面显示,就是SplashActivity. 目前在看到一个替代SplashActivity的方案. 使用SplashActivity的时候, ...
- Android Studio上的几个插件
转载:http://blog.csdn.net/maosidiaoxian/article/details/44992655 以下所有插件都可以在Idea的插件库中找到,如果你与我一样在Android ...
- 使用ssh公钥密钥自动登陆linux服务器
转自:http://7056824.blog.51cto.com/69854/403669 作为一名 linux 管理员,在多台 Linux 服务器上登陆进行远程操作是每天工作的一部分.但随着服务器的 ...
- uva1638Pole Arrangement
递推. 用f[n][l][r]表示n个柱子,从左面能看到l个,从右面能看到r个. 如果我们按照从小到大的顺序放置的话,放置最高的柱子后,大量状态都能递推到当前状态,很难写出递推式. 但是我们如果从小到 ...
- return File
public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...