动态规划——Russian Doll Envelopes
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只包含自身。
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的更多相关文章
- [LeetCode] 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 (Dynamic Programming)
https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...
- 【leetcode】354. Russian Doll Envelopes
题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...
- [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 ...
- 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] 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"
An intuitive DP - should be 'medium'. class Solution { public: int maxEnvelopes(vector<pair<in ...
随机推荐
- python—shutil模块
该模块拥有许多文件或文件的删除.移动.复制.重命名等功能. 1.copy():复制文件 格式:shutil.copy(来源文件,目标地址) 返回值:返回复制之后的路径 2.copy2():复制文件和状 ...
- Ubuntu18.04格式化U盘为NTFS的方法
1.先安装工具ntfs-3g sudo apt-get install ntfs-3g 2.找到需要格式化的U盘在系统中的序号 df -h 找到U盘的序列号后记下来,例如“/dev/sdd1” 3.解 ...
- Intellij IDEA 修改jsp 不能实时更新
Intellij IDEA 修改jsp 不能实时更新 1. 首先,output要指定到项目的webapp下,这样应该就可以实时更新了 2. 我的问题是这样设置之后,也不可以,原来是可以的,重装系统之后 ...
- centos 7.2下搭建vsftp 虚拟用户
虚拟用户搭建vsftp 要求一: 只允许上传 下载 不能删除 不能更换名称 yum install pam* yum install db4* -y yum install vsftpd chkcon ...
- spark2.2.1安装、pycharm连接spark配置
一.单机版本Spark安装 Win10下安装Spark2.2.1 1. 工具准备 JDK 8u161 with NetBeans 8.2: http://www.oracle.com/technetw ...
- 最大K段和题解
题目:XJOI335 传送门 [ >XJOI<] 重要提示:您的膜法等级必须达到3级6段才可使用本传送门,否则您会被小猫痛扁 因为博主太懒,不提供题面(QAQ)... 很容易想到使用DP, ...
- 生成唯一UUID
目前有多种方式生成的UUID,根据算法,可确定是否唯一,使用IP和MAC自定义生成唯一主键较妥: // 获取MAC地址的方法 private static String getMACAddress(I ...
- 011_TCP专项研究监控
(1)In Segs 数据源: /proc/net/snmp; 采集方式:累计值,每10秒采集一次取差值: 指标:net.snmp.tcp (key: system); In Segs: tcp协议层 ...
- 龙芯yl8089无声音的解决方案
网上搜索到的解决方法都是卸载pulseaudio,但这种方法比较暴力不能从根本上解决问题. 经过一段时间的排查,我发现最终问题出现在resample-method上. 由于内核内CS5536 AC97 ...
- Windows【端口被占用,杀死想啥的端口】
windows 两步方法 netstat -ano | findstr "8080" taskkill /pid 4136-t -f linux 两步方法 ps -ef | gre ...