这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃。这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大小,要求输出最多有几个信封能套在一起。
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]).
这个题细想其实很简单,首先要排序,按长从小到大排列,如果长一样,视宽小的为小排在前面。这样一来最优子结构和状态转移就都有了。
状态:dp[i]表示包含了第i的信封的ans,状态转移方程:如果第i个信封能套下它前面的第j个信封,则dp[i] = max{dp[j]+1},0<=j<i,否则dp[i] = 1只包含自身。
这个题的关键其实是前面排序那部分,我随便写了个冒泡O(n^2)的排序,还算运气好直接ac了。
 
 public int maxEnvelopes(int[][]envelopes) {
int esize = envelopes.length;
if(esize==0)return 0;
if(esize==1)return 1;
int ans = 1;
int t1 = 0,t2 = 0;
for(int i = esize-1;i>0;i--) {
for(int j = 0;j<i;j++) {
if(envelopes[j][0]>envelopes[j+1][0]) {
t1 = envelopes[j][0];
envelopes[j][0] = envelopes[j+1][0];
envelopes[j+1][0] = t1;
t2 = envelopes[j][1];
envelopes[j][1] = envelopes[j+1][1];
envelopes[j+1][1] = t2;
}else if((envelopes[j][0]==envelopes[j+1][0])&&(envelopes[j][1]>envelopes[j+1][1])) {
t2 = envelopes[j][1];
envelopes[j][1] = envelopes[j+1][1];
envelopes[j+1][1] = t2;
}
}
}
int[]dp = new int[esize];
Arrays.fill(dp,1);
for(int i = 1;i<esize;i++) {
for(int j = 0;j<i;j++)
if(envelopes[j][0]<envelopes[i][0] && envelopes[j][1]<envelopes[i][1])dp[i] = Math.max(dp[i],dp[j]+1);
ans = Math.max(ans, dp[i]);
}
return ans;
}
 

动态规划——Russian Doll Envelopes的更多相关文章

  1. [LeetCode] 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 (Dynamic Programming)

    https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...

  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. [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 ...

  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. 354 Russian Doll Envelopes 俄罗斯娃娃信封

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

  8. [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 ...

  9. LeetCode "Russian Doll Envelopes"

    An intuitive DP - should be 'medium'. class Solution { public: int maxEnvelopes(vector<pair<in ...

随机推荐

  1. WebGL&Three.js工作原理

    一.我们讲什么? 我们讲两个东西:1.WebGL背后的工作原理是什么?2.以Three.js为例,讲述框架在背后扮演什么样的角色? 二.我们为什么要了解原理? 我们假定你对WebGL已经有一定了解,或 ...

  2. VMware 虚拟机 linux执行 ifconfig 命令 eth0没有IP地址(intet addr、Bcast、Mask) UP BROADCAST MULTICAST 问题

    VMware 虚拟机 linux执行 ifconfig 命令 eth0没有IP地址(intet addr.Bcast.Mask) UP BROADCAST MULTICAST 问题 eth0:网络接口 ...

  3. [Jetty] Debugging With Eclipse

    There are a number of options available to debug your application in Eclipse. If not done already pr ...

  4. 浅入深出Vue:工具准备之PostMan安装配置及Mock服务配置

    浅入深出Vue之工具准备(二):PostMan安装配置 由于家中有事,文章没顾得上.在此说声抱歉,这是工具准备的最后一章. 接下来就是开始环境搭建了~尽情期待 工欲善其事必先利其器,让我们先做好准备工 ...

  5. Linux环境安装jdk12-rpm

    此博客只是本人的个人学习记录,若有什么误导观看者,请不要怪我... 1.环境准备: Linux环境:Centos7 jdk版本:JavaSE12      下载地址:https://www.oracl ...

  6. spring cloud--zuul网关和zuul请求过滤

    这里仍然以Windows和jdk为运行环境,按照下面的步骤打包-运行-访问就能看到效果.启动项目jar包: java -jar F:\jars-zuul\register-0.0.1-SNAPSHOT ...

  7. SVM小白教程(2):拉格朗日对偶

    在上一篇文章中,我们推导出了 SVM 的目标函数: \[ \underset{(\mathbf{w},b)}{\operatorname{min}} ||\mathbf{w}|| \\ \operat ...

  8. 题解 P4093 【[HEOI2016/TJOI2016]序列】

    这道题原来很水的? noteskey 一开始以为是顺序的 m 个修改,然后选出一段最长子序列使得每次修改后都满足不降 这 TM 根本不可做啊! 于是就去看题解了,然后看到转移要满足的条件的我发出了黑人 ...

  9. -bash: yum: command not found

    在学习Linux时,发现yum命令无法执行.便查了下,整理成文. yum介绍 Yum(全称为 Yellow dogUpdater, Modified)是一个在Fedora和RedHat以及CentOS ...

  10. java:构造方法:无参构造/有参构造 this static关键字 静态变量 静态方法 代码块 封装 静态常量。

    /*构造方法是一种特殊的方法,专门用于构造/实例化对象,形式:[修饰符] 类名(){ }构造方法根据是否有参数分为无参构造和有参构造*/public class Dog {               ...