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. Spring Security权限控制

    Spring Security官网 : https://projects.spring.io/spring-security/ Spring Security简介: Spring Security是一 ...

  2. <泛> STL - vector 模拟实现

    今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...

  3. STP协议树配置

    STP协议树作用 为了提高网络可靠性,交换网络中通常会使用冗余链路. 然而,冗余链路会给交换网络带来环路风险 并导致广播风暴以及MAC地址表不稳定等问题进而会影响到用户的通信质量. 生成树协议STP( ...

  4. 用profile协助程序性能优化

     程序运行慢的原因有很多,比如存在太多的劣化代码(如在程序中存在大量的“.”操作符),但真正的原因往往是比较是一两段设计并不那么良好的不起眼的程序,比如对一序列元素进行自定义的类型转换等.因为程序性能 ...

  5. 深入理解ajax系列第四篇

    前面的话 现代Web应用中频繁使用的一项功能就是表单数据的序列化,XMLHttpRequest 2级为此定义了FormData类型.FormData为序列化表单以及创建与表单格式相同的数据提供了便利. ...

  6. 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价

    python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...

  7. 线性表之单链表C++实现

    线性表之单链表 一.头文件:LinkedList.h //单链表是用一组任意的存储单元存放线性表的元素,这组单元可以是连续的也可以是不连续的,甚至可以是零散分布在内存中的任意位置. //单链表头文件 ...

  8. windows提权exp列表

    漏洞列表 #Security Bulletin #KB #Description #Operating System CVE-2017-0213 [Windows COM Elevation of P ...

  9. SLF4J versions 1.4.0 and later requires log4j 1.2.12 or later 终极解决

    http://blog.sina.com.cn/s/blog_54eb26870100uynj.html 到SLF4J官方网站:http://www.slf4j.org/codes.html#log4 ...

  10. ASP.NET MVC 手机短信验证

    本文来自于stoneniqiu的文章,原文地址 http://www.cnblogs.com/stoneniqiu/p/6234002.html 1.注册一个应用 得到AppKey 和 App Sec ...