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信封,现在给定一组信封的大 ...
随机推荐
- apache&nginx资料汇总
http://liudaoru.iteye.com/blog/336338 aquid:http://os.51cto.com/art/201009/225813.htm 数据库各种讲座:http:/ ...
- DoG 、Laplacian、图像金字塔详解
DoG(Difference of Gaussian) DoG (Difference of Gaussian)是灰度图像增强和角点检测的方法,其做法较简单,证明较复杂,具体讲解如下: Differe ...
- 写给系统管理员的25个PHP安全实践
PHP是广泛使用的开源服务端脚本语言.通过HTTP或HTTPS协议,Apache Web服务允许用户访问文件或内容.服务端脚本语言的错误配置会导致各种问题.因此,PHP应该小心使用.以下是为系统管理员 ...
- nyoj-291 互素数个数 欧拉函数
LK的数学题 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 LK最近遇到一个问题,需要你帮她一下.一个整数n,求[1,n)中,和n互素的数的个数. 输入 多组测 ...
- Android studio中Rendering Problems不能可视化操作的解决办法
出现:Rendering Problems the following classes could not be found:android.support.v7.internal.widget.Ac ...
- HDU 1255 覆盖的面积 (扫描线 线段树 离散化 矩形面积并)
题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了, ...
- uvaIrrelevant Elements
唯一分解定理. 可以看出在最后每个a的系数是杨辉三角的第n行. 但是不能递推,否则会tle. 就从C(n-1,0)开始乘n-k再除以k.记录下每个的系数,如果该项系数小于m就代表和答案有关. 代码里的 ...
- Linux shell下批量创建缩略图
一.背景 今天,突然发现手机客户端上的最新新闻缩略图都不显示了,上服务器上看了看, 发现新的新闻图片根本没有生成缩略图. 这套新闻发布系统是很老的程序了,查了一下,问题的原因是不支持png格式的图片, ...
- highcharts 柱状图动态设置数据应用实例
<div id="container" style="min-width:700px;height:400px"></div> #jav ...
- shell脚本实例
备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...