这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃。这个题是大信封套小信封,每个信封都有长和宽,如果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. python—shutil模块

    该模块拥有许多文件或文件的删除.移动.复制.重命名等功能. 1.copy():复制文件 格式:shutil.copy(来源文件,目标地址) 返回值:返回复制之后的路径 2.copy2():复制文件和状 ...

  2. Ubuntu18.04格式化U盘为NTFS的方法

    1.先安装工具ntfs-3g sudo apt-get install ntfs-3g 2.找到需要格式化的U盘在系统中的序号 df -h 找到U盘的序列号后记下来,例如“/dev/sdd1” 3.解 ...

  3. Intellij IDEA 修改jsp 不能实时更新

    Intellij IDEA 修改jsp 不能实时更新 1. 首先,output要指定到项目的webapp下,这样应该就可以实时更新了 2. 我的问题是这样设置之后,也不可以,原来是可以的,重装系统之后 ...

  4. centos 7.2下搭建vsftp 虚拟用户

    虚拟用户搭建vsftp 要求一: 只允许上传 下载 不能删除 不能更换名称 yum install pam* yum install db4* -y yum install vsftpd chkcon ...

  5. spark2.2.1安装、pycharm连接spark配置

    一.单机版本Spark安装 Win10下安装Spark2.2.1 1. 工具准备 JDK 8u161 with NetBeans 8.2: http://www.oracle.com/technetw ...

  6. 最大K段和题解

    题目:XJOI335 传送门 [ >XJOI<] 重要提示:您的膜法等级必须达到3级6段才可使用本传送门,否则您会被小猫痛扁 因为博主太懒,不提供题面(QAQ)... 很容易想到使用DP, ...

  7. 生成唯一UUID

    目前有多种方式生成的UUID,根据算法,可确定是否唯一,使用IP和MAC自定义生成唯一主键较妥: // 获取MAC地址的方法 private static String getMACAddress(I ...

  8. 011_TCP专项研究监控

    (1)In Segs 数据源: /proc/net/snmp; 采集方式:累计值,每10秒采集一次取差值: 指标:net.snmp.tcp (key: system); In Segs: tcp协议层 ...

  9. 龙芯yl8089无声音的解决方案

    网上搜索到的解决方法都是卸载pulseaudio,但这种方法比较暴力不能从根本上解决问题. 经过一段时间的排查,我发现最终问题出现在resample-method上. 由于内核内CS5536 AC97 ...

  10. Windows【端口被占用,杀死想啥的端口】

    windows 两步方法 netstat -ano | findstr "8080" taskkill /pid 4136-t -f linux 两步方法 ps -ef | gre ...