这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃。这个题是大信封套小信封,每个信封都有长和宽,如果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. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

  2. Redux thunk中间件

    redux-thunk https://github.com/reduxjs/redux-thunk Why Do I Need This? Thunks are the recommended mi ...

  3. Java 集合系列08之 List总结

    一.List概述 1. List是一个接口,它继承于Collection接口,代表有序集合 2. ArrayList, LinkedList, Vector, Stack是List的4个实现类. Ar ...

  4. MySQL保留字不能作为字段名使用

    在设计MySQL字段的时候,无意中使用InOut这个名称作为字段名称,结果前端提交后就是没有写入数据库!但后端没有任何提示,跟踪mySQL日志,也没有留下痕迹,反复查,不得其解. 后来实在没有办法情况 ...

  5. Python 面试总结

    公司面试: 1,说说项目都用到了什么技术? 2,mysql索引的种类? 3,索引建多有什么不好? 4,mysql的引擎有什么? 5,redis是单线程还是多线程的? 6, redis的持久化机制? 7 ...

  6. 生成唯一UUID

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

  7. nb-iot简介【转】

    转自:http://www.elecfans.com/tags/nb-iot/ 标签 > nb-iot nb-iot 关注 118人关注 提供NB-IoT技术特点,NB-IoT模块/芯片,NB- ...

  8. js数据结构与算法——集合

    <script> function Set(){ var items = {};//使用对象表示集合,因为js对象不允许一个键指向两个不同的值,保证集合里面的匀速唯一性 this.add ...

  9. systemctl: command not found

    可以使用service代替 service语法有一点区别 这里演示了错误语法和正确语法

  10. C# 高级编程04----类

    一.类和结构: 相同点: 1. 类和结构都是创建对象的模板,包含数据成员和方法成员 不同点: 1.在内存中的存储方式: 1)类是存储在堆内存上的引用类型,结构是存储在栈内存的值类型 2)结构不支持继承 ...