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入门1(简介、安装)

    第一次写博客,希望自己能养成每天一篇的好习惯(表示有很大的挑战,就不立Flag了) 先从Python开始吧.下面是本人所学习的主要教材电子版: 链接:http://pan.baidu.com/s/1e ...

  2. ArrayList源码中EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA的区别

    2018年7月22日09:54:17 JDK 1.8.0_162 ArrayList源码中EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA的区别 ...

  3. perf工具crash的问题

    perf抓取时系统crash的情况.找前同事了解到perf工具导致系统crash的一种情况, perf工具默认是使用cycles,这个硬件事件是使用NMI,可能会导致内核错误. 之前文档上的perf命 ...

  4. poj-3268最短路

    title: poj-3268最短路 date: 2018-10-13 15:54:34 tags: acm 刷题 categories: ACM-最短路 概述 这是一道最短路的模板题,,,不过虽然是 ...

  5. C++ 几种经典的垃圾回收算法

    之前遇到了一篇好文(https://blog.csdn.net/wallwind/article/details/6889917)准备学习一下的,课程繁忙就忘记了,今日得闲,特来补一下. 自己写一遍加 ...

  6. 获取类的属性并排除特定属性(getType().GetProperties())

    当获取一个类型(class)的所有属性时,想排除指定属性,该如何操作? 比如:EF中一个实体类型UserEntity,通过反射获取这个类的属性时,想排除这个为映射的字段ID 使用以下方法即可! Pro ...

  7. Android DecorView浅析

    摘要 一.DecorView为整个Window界面的最顶层View. 二.DecorView只有一个子元素为LinearLayout.代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域. ...

  8. s3c2440地址分配

    mini2440的地址怎么分配.mini2440处理器的地址怎么分配. S3C2440处理器可以使用的物理地址空间可以达到4GB,其中前1GB的地址为连接外设的地址空间.>1G的地址空间 分配给 ...

  9. bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息

    1590: [Usaco2008 Dec]Secret Message 秘密信息 Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共 ...

  10. [OpenGL]纹理贴图实现 总结

    实现步骤 第一步:设置所需要的OpenGL环境 设置上下文环境 删除已经存在的渲染的缓存 设置颜色缓存 设置帧缓存 清除缓存 设置窗口大小 开启功能 编译shander 使用program 获取sha ...