russian-doll-envelopes

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 == 0) {
            return 0;
        }

        // loop from big envelope to small envelope
        for (int i=vlen-1; i>=0; 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 + 1) == mp.end()) {
                            vector<pair<int, int>> tmpvec;
                            tmpvec.push_back(envelopes[i]);
                            mp[itr->first + 1] = tmpvec;
                        }
                        else {
                            mp[itr->first + 1].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(1) == mp.end()) {
                    vector<pair<int, int>> tmpvec;
                    tmpvec.push_back(envelopes[i]);
                    mp[1] = tmpvec;
                }
                else {
                    mp[1].push_back(envelopes[i]);
                }
            }
        }

        // return the most Russian doll answer
        return mp.rbegin()->first;
    }
};
时间: 2024-10-12 19:42:12

russian-doll-envelopes的相关文章

leetCode 354. Russian Doll Envelopes

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. What is

[email&#160;protected] [354] Russian Doll Envelopes (Dynamic Programming)

https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater tha

【leetcode】354. Russian Doll Envelopes

题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. Wh

【Leetcode】Russian Doll Envelopes

题目链接:https://leetcode.com/problems/russian-doll-envelopes/ 题目: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is gr

动态规划——Russian Doll Envelopes

这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大小,要求输出最多有几个信封能套在一起. Example:Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] =>

[LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than

LeetCode &quot;Russian Doll Envelopes&quot;

An intuitive DP - should be 'medium'. class Solution { public: int maxEnvelopes(vector<pair<int, int>>& envelopes) { int n = envelopes.size(); if (!n) return 0; if (n == 1) return 1; // Sort by Area sort(envelopes.begin(), envelopes.end(),

354. Russian Doll Envelopes

从我花时间上看,我是纠结排序很久.注意一下Comparator的写法,以后会了就好~ 算法上讲,就是对envolope尺寸排序,然后对于排序后的每个信封,它可以装进的最多小信封数,是长宽都比它小的信封中装的进最多数目+1.和之前做的368. Largest Divisible Subset思路是一样的,甚至还要简单一点. 动规. 1 static class MySort implements Comparator<int[]> { 2 public int compare(int[] o1,

第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小排序. 维护一个符合题意的队列,当队列中的第二维均比当前信封的第二维小时,必然可以增加到队尾. 如果不然,可以让当前信封作为"替补",它可以在恰当的时候代替恰好比它大的信封. 当替补们足够替换所有已有信封时,就可以增加新的信封了. 比较抽象,不过这个技巧很有趣. 看代码吧,很清晰. cla

[动态规划] leetcode 354 Russian Doll Envelopes

problem:https://leetcode.com/problems/russian-doll-envelopes/ 最长连续子序列类型问题.先排序,dp[i]记录使用第i个套娃的最大数量. bool cmp(const vector<int>& x, const vector<int>& y) { return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0]; } class Solution { public: