码迷,mamicode.com
首页 > 其他好文 > 详细

354 Russian Doll Envelopes 俄罗斯娃娃信封

时间:2018-04-15 11:37:28      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:air   and   rand   cto   max   put   only   begin   blog   

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 the maximum number of envelopes can you Russian doll? (put one inside other)
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] => [6,7]).

详见:https://leetcode.com/problems/russian-doll-envelopes/description/

C++:

class Solution {
public:
    int maxEnvelopes(vector<pair<int, int>>& envelopes) {
        int res = 0, n = envelopes.size();
        vector<int> dp(n, 1);
        sort(envelopes.begin(), envelopes.end());
        for (int i = 0; i < n; ++i) 
        {
            for (int j = 0; j < i; ++j) 
            {
                if (envelopes[i].first > envelopes[j].first && envelopes[i].second > envelopes[j].second) 
                {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            res = max(res, dp[i]);
        }
        return res;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5568818.html

354 Russian Doll Envelopes 俄罗斯娃娃信封

标签:air   and   rand   cto   max   put   only   begin   blog   

原文地址:https://www.cnblogs.com/xidian2014/p/8837190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!