2017年5月21日12:35:53354. Russian Doll Envelopes

来源:互联网 发布:淘宝助理导出宝贝 编辑:程序博客网 时间:2024/06/11 14:18

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]).

俄罗斯瓷娃娃问题
要是长宽比之前的小就可以套 
bool cmp (pair<int, int> i, pair<int, int> j) {        if (i.first == j.first)            return i.second < j.second;        return i.first < j.first;    }        class Solution {    public:        int maxEnvelopes(vector<pair<int, int>>& envelopes) {            int N = envelopes.size();            vector<int> dp(N, 1);            int mx = (N == 0) ? 0 : 1;            sort(envelopes.begin(), envelopes.end(), cmp);            for (int i = 0; i < N; i++) {                for (int j = i - 1; j >= 0; j--) {                    if (envelopes[i].first > envelopes[j].first && envelopes[i].second > envelopes[j].second) {                        dp[i] = max(dp[i], dp[j] + 1);                        mx = max(dp[i], mx);                    }                }            }            return mx;        }    };
给出一个长宽找出最长的 再找最宽 然后根据这个长宽找其余最大长宽 时间复杂度 n^2
原创粉丝点击