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. Python 笔记(一)字典与json使用及注意点

    个人笔记系列,随便参考 1.python 中字典与json的差别 字典的生成 >>> a = dict(one=1, two=2, three=3) >>> b = ...

  2. matlab .fig转化成pdf 缺失

    1. 在matlab figure里面,Edit -> Copy Figure 2. 粘贴到ppt中即可

  3. mysql 存储过程详解

    MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...

  4. spring中的scope详解

    spring容器中的bean默认是单例模式的,改成非单例模式需要在类上加上@Scope("prototype") 1. scope概论 spring中scope是一个非常关键的概念 ...

  5. JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现

    1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...

  6. PHP cURL中CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT的区别

    CURLOPT_CONNECTTIMEOUT用来告诉PHP脚本在成功连接服务器前等待多久(连接成功之后就会开始缓冲输出),这个参数是为了应对目标服务器的过载,下线,或者崩溃等可能状况: CURLOPT ...

  7. android 消息机制,handler机制,messageQueue,looper

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha handler  就是 处理器 .  用来处理消息, 发送消息. handler   就 ...

  8. [BZOJ4569][SCOI2016]萌萌哒(倍增+并查集)

    首先有一个显然的$O(n^2)$暴力做法,将每个位置看成点,然后将所有限制相等的数之间用并查集合并,最后答案就是9*(10^连通块的个数).(特判n=1时就是10). 然后比较容易想到的是,由于每次合 ...

  9. 【推导】The 16th UESTC Programming Contest Preliminary L - Foxtrot

    题意:有n瓶药剂,其中只有一瓶药剂有毒.让你用最少的小白鼠试出哪瓶有毒.你只有一次给任意只小白鼠各喂食任意种类药剂的机会. m只老鼠就能对应2^m种“生死状态”的组合,给每种状态分配一个种类的药剂,然 ...

  10. Makefile-fPIC,C++静态库与动态库

    在计算机领域中,地址无关代码 (英文: position-independent code,缩写为PIC),又称地址无关可执行文件 (英文: position-independent executab ...