动态规划——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 ...
随机推荐
- table自适应大小,以及内容换行
在table的样式中加入以下两个样式: table-layout: fixed; word-wrap:break-word;
- 作业二Wordcount
1,github地址 https://github.com/dtneverdie/word-count 2,PSP表格 3,解题思路 先从理论上判断应该先将文件内的字符全部读入,然后根据分隔符来进行单 ...
- HTTP常见的状态码——面试题常考
一些常见的状态码为: 200 - 服务器成功返回网页 400(错误请求)服务器不理解请求的语法. 404 - 请求的网页不存在 500(服务器内部错误)服务器遇到错误,无法完成请求. 503 - 服 ...
- 怎样解决canvas 插件 和html 页面中的事件冲突问题 ?
很简单 ,在html 执行事件所在的div中 设置 position:relative;
- Contest2163 - 2019-3-28 高一noip基础知识点 测试6 题解版
传送门 @dsfz201814 改题 T1:全锕,过 T2:全锕,过 T3:@dsfz201814 先用竖着放置的木块将它变成高度差最大为1的数列 然后对于任意相邻相等的两块,可以将它看成任意 例如, ...
- neo4j语法
图数据库在社交网络.实时推荐.征信系统.人工智能等领域有广泛应用. 集群特征:主从复制,重选主服务器和容错:每个实例都有自己的本地缓冲 性能优势:查询内不跨网络:实时操作,具有快速和一致的响应时间:缓 ...
- ftp和mysql数据库结合使用
问题描述: 看下 1.1.1.1 的ftp为什么连不上 报错的信息: 'ftpServer' => '1.1.1.1', // FTP服务器地址 ', 'ftpUsername' => ' ...
- [SDOI2009]HH的项链-树状数组/线段树
树状数组: #include<bits/stdc++.h> using namespace std; ; int id[maxn],tree[maxn],vis[maxn],num[max ...
- idea工具maven生命周期clean,compile,install,package区别
idea工具maven projects里面有9种生命周期,今天刚好遇到,顺便分享下自己的理解.生命周期是包含在一个项目构建中的一系列有序的阶段.最常用的两种打包方法:一:clean,package( ...
- 以慕课网日志分析为例-进入大数据Spark SQL的世界
下载地址.请联系群主 第1章 初探大数据 本章将介绍为什么要学习大数据.如何学好大数据.如何快速转型大数据岗位.本项目实战课程的内容安排.本项目实战课程的前置内容介绍.开发环境介绍.同时为大家介绍项目 ...