题目:

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Note:
Rotation is not allowed.

Example:

Input: [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

代码:

方法一——超时的:

class Solution {
public:bool doll(vector<int>& a,vector<int>& b){return a[0]<b[0]&&a[1]<b[1];}int maxEnvelopes(vector<vector<int>>& envelopes) {int len=envelopes.size();if(len<=0)return 0;map<vector<int>,int>m;sort(envelopes.begin(),envelopes.end());m[envelopes[0]]=0;int record=0;for(int i=1;i<len;i++){for(int j=i-1;j>=0;j--){if(doll(envelopes[j],envelopes[i])){m[envelopes[i]]=max(m[envelopes[i]],m[envelopes[j]]+1);record=max(record,m[envelopes[i]]);}}}return record+1;}
};

方法二——思路和上一个相同,但是dp数组是坐标形式,就会快不少。:

class Solution {
public:int maxEnvelopes(vector<vector<int>>& envelopes) {int res = 0, n = envelopes.size();vector<int> dp(n, 1);sort(envelopes.begin(), envelopes.end());for (int i = 0; i < n; ++i) {for (int j = 0; j < i; ++j) {if (envelopes[i][0] > envelopes[j][0] && envelopes[i][1] > envelopes[j][1]) {dp[i] = max(dp[i], dp[j] + 1);}}res = max(res, dp[i]);}return res;}
};

方法三——固定一个,另外一个使用二分法

class Solution {
public:int maxEnvelopes(vector<vector<int>>& envelopes) {vector<int> dp;sort(envelopes.begin(), envelopes.end(), [](const vector<int>&a, const vector<int>&b){if (a[0] == b[0]) return a[1] > b[1];return a[0] < b[0];});for (int i = 0; i < envelopes.size(); ++i) {int left = 0, right = dp.size(), t= envelopes[i][1];while (left < right) {int mid = left + (right - left) / 2;if (dp[mid] < t) left = mid + 1;else right = mid;}if (right >= dp.size()) dp.push_back(t);else dp[right] = t;}return dp.size();}
};

Leetcode之Russian Doll Envelopes相关推荐

  1. leetcode 354. Russian Doll Envelopes

    题目链接:https://leetcode.com/problems/russian-doll-envelopes/   俄罗斯套娃o(╯□╰)o 方法一 递归 思想:先对套娃从大到小进行排序,然后依 ...

  2. leetcode(354)—— Russian Doll Envelopes(俄罗斯套娃信封)

    原题位置:Russian Doll Envelopes | LeetCode OJ 俄罗斯套娃信封问题的本质是一个二维版的 LIS(最长递增子列)的问题.为了更好地理解俄罗斯套娃信封问题的动态规划思路 ...

  3. leetcode算法题--Russian Doll Envelopes

    原题链接:https://leetcode.com/problems/russian-doll-envelopes/ class Solution {public:struct node {int x ...

  4. LeetCode--354. Russian Doll Envelopes

    问题链接:https://leetcode.com/problems/russian-doll-envelopes/ 俄罗斯套娃问题,因垂丝汀!!!,要求俄罗斯套娃能够达到的最多嵌套的层数.先要理解对 ...

  5. 354. Russian Doll Envelopes刷题笔记

    题解链接,用dp做 class Solution:def maxEnvelopes(self, envelopes: List[List[int]]) -> int:envelopes.sort ...

  6. 寒假LeetCode打卡

    文章目录 @[toc] 链表专题 LeetCode 19. Remove Nth Node From End of List LeetCode 83. Remove Duplicates from S ...

  7. taoqick 搜索自己CSDN博客

    L1 L2正则化和优化器的weight_decay参数 kaiming初始化的推导 Pytorch动态计算图 Pytorch自动微分机制 PyTorch中在反向传播前为什么要手动将梯度清零? 通俗讲解 ...

  8. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

  9. LeetCode 从零单刷个人笔记整理(持续更新)

    更新至2020.2.23 github:https://github.com/ChopinXBP/LeetCode-Babel 本人博客用于个人对知识点的记录和巩固. 用几乎所有可行的方法进行了实现和 ...

最新文章

  1. Remix中文版 【Solidity IDE】
  2. 鹅厂最新数字人,体温36.5℃
  3. go语言笔记——是c开发的 lex yacc进行词法和语法分析,go不支持函数和运算符重载,不支持类型继承,也不支持断言,还有泛型...
  4. 浅谈equals和==的区别
  5. Tomcat 7.0~10.0zip版,安装版,其他一键式全部下载
  6. boost::type_erasure模块construction相关的测试程序
  7. WebDriver自动化测试框架详解
  8. iSCSI 2-环境搭建一
  9. java用循环语法在窗体中显示文字,如果子窗体在窗体视图中,则访问2003循环当前页面不起作用...
  10. python GUI编程tkinder
  11. java内存管理的一些基础,内存溢出的解决方案
  12. Jquery 安装到Visual Studio 2008
  13. SLAM会议笔记(四)Lego-LOAM
  14. pytorch ImageFolder
  15. 软件著作权算法软件设计说明书_软件详细设计说明书例子.pdf
  16. log4j 日志输出级别区别
  17. 为什么 Mac 适合编程?
  18. Netty实现自定义协议和源码分析
  19. mysql+一直running_mysql 事务一直running问题排查
  20. javascript汉字转拼音 [zt]

热门文章

  1. 国足历届世界杯对战图关系
  2. stm32驱动sh36730x的驱动代码
  3. 阿里 c++ 编码规范 百度网盘_阿里云网盘,内测资格,开放申请了!非会员下载 10MB/s...
  4. Ubuntu1804下的Melodic版本Moveit和OMPL的源码安装,并自定义规划算法在Moveit上使用
  5. 《乔布斯传》圈点(11)
  6. AjaxUpLoad.js文件上传插件的使用
  7. 微软Windows Azure项目交流会小记
  8. aaron note mysql
  9. 量子计算机定义及组成,量子控制的基本概念及其哲学意义
  10. [导入]猪年新年贺词猪年短信猪年祝福语