Skip to content

Latest commit

 

History

History
31 lines (27 loc) · 1.06 KB

354.md

File metadata and controls

31 lines (27 loc) · 1.06 KB

要注意,排序完并不是严格的大于关系,加一层等于的判断

这题的nlogn DP倒是挺有意思的 之前没有认真想 https://discuss.leetcode.com/topic/47469/java-nlogn-solution-with-explanation/9

如果当前出现的数小于之前的某一个就直接踢掉之前那个,反正没软用。如果加在最后一位就把count+1咯!

class Solution {
    typedef pair<int, int> P;
    static bool cmp(const P a, const P b){
        return a.first < b.first;
    }

public:
    int maxEnvelopes(vector<pair<int, int>>& envelopes) {
        sort(envelopes.begin(), envelopes.end(), cmp);
        vector<int> count(envelopes.size(), 1);
        for(int i = 1; i < envelopes.size(); ++i){
            for(int j = 0; j < i; ++j){
                if(envelopes[j].second < envelopes[i].second && envelopes[j].first != envelopes[i].first)
                    count[i] = max(count[i], count[j]+1);
            }
        }
        int ans = 0;
        for(int i = 0; i < envelopes.size(); ++i)
            ans = max(ans, count[i]);
        return ans;
    }
};