https://leetcode.com/problems/russian-doll-envelopes/

// Use map (Russian doll number -> vector of envelopes) to record results
// For each envelope, check above map, and when fitting envelope which can hold current one is found,
// no need to check more envelope as later envelope is with smaller size or smaller Russian doll number. bool lesser(const pair<int, int> &m1, const pair<int, int> &m2) {
// sort function
return m1.first < m2.first || (m1.first == m2.first && m1.second < m2.second);
} class Solution {
// Russian doll number -> vector of envelopes
map<int, vector<pair<int, int>>> mp;
map<int, vector<pair<int, int>>>::reverse_iterator itr;
vector<pair<int, int>>::iterator vitr;
// bool for whether best answer is reached
bool fit;
public:
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(), lesser);
int vlen = envelopes.size();
if (vlen == ) {
return ;
} // loop from big envelope to small envelope
for (int i=vlen-; i>=; i--) {
// with the order of Russian doll, check whether current envelope can fit previous one
for(itr = mp.rbegin(); itr != mp.rend(); ++itr) {
fit = false;
for (vitr = itr->second.begin(); vitr != itr->second.end(); ++vitr) {
if (envelopes[i].first < (*vitr).first &&
envelopes[i].second < (*vitr).second) { // find fitting envelope, add one to Russian doll answer and record
if (mp.find(itr->first + ) == mp.end()) {
vector<pair<int, int>> tmpvec;
tmpvec.push_back(envelopes[i]);
mp[itr->first + ] = tmpvec;
}
else {
mp[itr->first + ].push_back(envelopes[i]);
}
fit = true;
break;
}
}
if (fit) {
// if current envelope fit Russian doll, no need to check envelope with less answer
break;
}
}
if (itr == mp.rend()) {
// if no fitting envelope, current envelope is with Russian doll answer 1
if (mp.find() == mp.end()) {
vector<pair<int, int>> tmpvec;
tmpvec.push_back(envelopes[i]);
mp[] = tmpvec;
}
else {
mp[].push_back(envelopes[i]);
}
}
} // return the most Russian doll answer
return mp.rbegin()->first;
}
};

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. 动态规划——Russian Doll Envelopes

    这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大 ...

  6. [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 ...

  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. 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] 354. Russian Doll Envelopes 俄罗斯套娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  10. LeetCode "Russian Doll Envelopes"

    An intuitive DP - should be 'medium'. class Solution { public: int maxEnvelopes(vector<pair<in ...

随机推荐

  1. linux下更换pip源

    pip不更换源的话,速度可能非常慢.这里将pip源更换为阿里云源. 1.修改文件~/.pip/pip.conf(没有该文件则创建一个) $ sudo vim ~/.pip/pip.conf 2.写入以 ...

  2. ref:下一个项目为什么要用 SLF4J

    ref:http://blog.mayongfa.cn/267.html 阿里巴巴 Java 开发手册 前几天阿里巴巴在云栖社区首次公开阿里官方Java代码规范标准,就是一个PDF手册,有命名规范,让 ...

  3. 关于table边框,设置了border-collapse:collapse之后,设置border-radius没效果

    做项目遇到边框需要设置圆角,然后发现在设置了border-collapse:collapse之后,border-radius:10px不起作用了,发现这个是css本身的问题,两者不能混在一起使用. 代 ...

  4. TCP 建立连接为什么要握 3 次手?

    上次已经说过,没有协议,不成方圆,计算机之间的通信更是依赖于协议.今天就重点分析一下 TCP 协议. 传输控制协议 TCP 是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 7 ...

  5. openstack policy 鉴权过程分析

    转:http://blog.chinaunix.net/uid-20940095-id-4144300.html 1. openstack 鉴权简单介绍       众所周知,openstack通过k ...

  6. 阿里云腾讯云服务器ubuntu多域名配置

    1.将域名A记录解析到服务器的公网 IP 地址,把www也添加A记录到公网ip 2.ubuntu系统修改hosts文件,hosts文件目录为/etc/hosts,可以用vim编辑  sudo vim ...

  7. lua协程----ngx-lua线程学习笔记

    --[[ - @desc lua数据输出 - @param string 字符串 - return string --]] function dump(v) if not __dump then fu ...

  8. python学习笔记5.1-核心类型-集合set类型[转]

    转自:http://blog.csdn.net/business122/article/details/7541486 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系 ...

  9. cocos2d-x3.0 macOS下配置Android开发环境以及使用cocos2d-console来新建执行project

    下面是子龙山人录制的关于cocos2d-x3.0的视频教程,macOS下配置Android开发环境.使用cocos2d-console来新建执行project.怎样执行cocos2d-x 3.0win ...

  10. js:深入prototype(上:内存分析)

    /**  * 下面演示了通过原型的创建方式,使用基于原型的创建能够将属性和方法  * 设置为Person专有的,不能通过window来调用.  * 原型是javascript中的一个特殊对象,当一个函 ...