leetcode 354. Russian Doll Envelopes

来源:互联网 发布:手机淘宝怎么申请品牌 编辑:程序博客网 时间:2024/05/16 13:50

题目链接:https://leetcode.com/problems/russian-doll-envelopes/   俄罗斯套娃o(╯□╰)o


方法一 递归

思想:先对套娃从大到小进行排序,然后依次找能够塞进大套娃的小套娃。由于套娃数量很多,大小不一,显而易见有个问题:遇到两个大小相似且不套住彼此的套娃,到底要选择哪一个!选择哪一个才能让总体的套娃数量最多呢?answer is here:递归!讲所有的可能性搞出来。

知识点:(1)sort:左大于右就是从大到小排序(2)vector:begin() end() size() (3)pair:first,secode (4)递归:从起点开始for循环,找到符合要求的小套娃则进入下一层递归。 比如测试用例为[9,8] [7,6] [7,5] [5,5],从[9,8]开始for循环,首先找到[7,6],对[7,6]进行递归,然后找到[7,5],对[7,5]进行递归,然后。。。。

[9,8]->[7,6]->[5,5]

[9,8]->[7,5]

[9,8]->[5,5]

代码:

class Solution {
public:
    static bool compare(pair<int, int>left, pair<int, int>right) {
        if (left.first > right.first) {
            return true;
        } else if (left.first == right.first && left.second > right.second) {
            return true;
        }
        return false;
    }
    
    int maxEnvelopes(vector<pair<int, int>>& envelopes) {
        sort(envelopes.begin(), envelopes.end(), compare);
        int max_num = 0;
        find_next_envelope(envelopes, 0, 1, max_num);
        return max_num;
    }
    
    void find_next_envelope(vector<pair<int, int>> sorted_envelopes, int curr, int envelope_num, int& max_num) {
        if (envelope_num > max_num) {
            max_num = envelope_num;
        }
        for (int i = curr + 1; i < sorted_envelopes.size(); i++) {
            if (sorted_envelopes[i].first < sorted_envelopes[curr].first
                    && sorted_envelopes[i].second < sorted_envelopes[curr].second) {
                find_next_envelope(sorted_envelopes, i, envelope_num + 1, max_num);
            }
        }
    }
};

 

方法二 动态规划

方法一在leetcode上超时了,时间复杂度太高了,似乎是O(2^n)?于是搞粗来了这种方法!

思想:这种方法主要是解决上面的重复工作问题。假设i为起点,先找到从i到end的max_num。蓝后如果i -1 为起点,那么狠容易找到i-1到end的max_num了。so,有一个result的vector,存储从每个点到end的max_num。从倒数第二个点开始依次向前for循环,找到该点到end的max_num。

1 2 3    4       5         6

       i max4 max5 max6

假设i = 3,第二层for循环要从4开始,到end结束。 max3 = 3 + maxj(if 3 可以套在j 外面)

知识点:vector<int> result(envelopes.size(), 1); 原谅我以前不会用vector

代码:

class Solution {
public:
    static bool compare(pair<int, int>left, pair<int, int>right) {
        if (left.first > right.first) {
            return true;
        } else if (left.first == right.first && left.second > right.second) {
            return true;
        }
        return false;
    }
    
    int maxEnvelopes(vector<pair<int, int>>& envelopes) {
        if (envelopes.size() == 0) {
            return 0;
        }
        sort(envelopes.begin(), envelopes.end(), compare);
        int max_num = 1;
        vector<int> result(envelopes.size(), 1);
        for (int i = envelopes.size() - 2; i >= 0; i--) {
            for (int j = i + 1; j < envelopes.size(); j++) {
                if (envelopes[i].first > envelopes[j].first && envelopes[i].second > envelopes[j].second) {
                    result[i] = max(result[j] + 1, result[i]);
                }
            }
            max_num = max(max_num, result[i]);
        }
        
        return max_num;
    }
};

0 0
原创粉丝点击