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. ECshop语言包lang的加载原理

    当前使用的ecshop的版本:2.7.3,ecshop 2.7.3版本的网店系统的语言包的位置是ecshop文件下 languages/xxx/   其中的xxx表示各种语言的文件夹,里面存放指定语言 ...

  2. OpenGL笔记<5> shader 调试信息获取 Debug

    我们今天来讲调试信息,这个东西讲起来会比较无聊,因为都是一些函数调用,没啥可讲的,函数就是那样用的,不过其效果挺好玩的,同时在程序设计中也是很必要的,所以还是来写一下,不过,就是因为知识比较固定且简单 ...

  3. luoguP4503 [CTSC2014]企鹅QQ hash

    既然只有一位的不同,那么我们可以枚举这一位.... 我们只需要快速地计算去掉某一位的$hash$值.... 由于$hash(S) = \sum s[i] * seed^i$,因此去掉第$i$位的权值只 ...

  4. BZOJ.5311.贞鱼(DP 决策单调)

    题目链接 很容易写出\(O(n^2k)\)的DP方程.然后显然决策点是单调的,于是维护决策点就可以了.. 这个过程看代码或者别的博客吧我不写了..(其实是忘了) 这样复杂度\(O(nk\log n)\ ...

  5. 汇编代码中db,dw,dd的区别

    db定义字节类型变量,一个字节数据占1个字节单元,读完一个,偏移量加1 dw定义字类型变量,一个字数据占2个字节单元,读完一个,偏移量加2 dd定义双字类型变量,一个双字数据占4个字节单元,读完一个, ...

  6. [COGS2580]偏序 II

    [COGS2580]偏序 II 题目大意: \(n(n\le50000)\)个五元组,求五维偏序. 思路: CDQ分治套CDQ分治套CDQ分治套树状数组. 时间复杂度\(\mathcal O(n\lo ...

  7. hdu 3338 最大流 ****

    题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...

  8. python MySQL 获取全部数据库(DATABASE)名、表(TABLE)名

    import MySQLdb #connect try: conn = MySQLdb.connect( host = "localhost", user = "root ...

  9. SGU 404 Fortune-telling with camomile

    404. Fortune-telling with camomile Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes ...

  10. RTSP交互过程

    步骤一: 发送:OPTIONS rtsp://127.0.0.1/172.30.31.225:8000:HIK-DS8000HC:0:1:admin:hs123456:av_stream RTSP/1 ...