[Dynamic Programming] leetcode 354 Russian Doll Envelopes

problem:https://leetcode.com/problems/russian-doll-envelopes/

        The longest contiguous subsequence type of problem. To sort, dp [i] records using the i th maximum number of doll.

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:

    int maxEnvelopes(vector<vector<int>>& envelopes) {
        sort(envelopes.begin(), envelopes.end(), cmp);
        int n = envelopes.size();
        if(!n) return 0;
        vector<int> dp(n, 1);
        int res = 0;
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < i;j++)
            {
                if(envelopes[j][0] == envelopes[i][0]) break;
                if(envelopes[i][1] > envelopes[j][1])
                {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            res = max(dp[i], res);
        }
        return res;
    }
};

 

Guess you like

Origin www.cnblogs.com/fish1996/p/11332509.html