zoukankan      html  css  js  c++  java
  • 354. Russian Doll Envelopes

    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)

    Note:
    Rotation is not allowed.

    Example:

    Input: [[5,4],[6,4],[6,7],[2,3]]
    Output: 3 
    Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
    
     

    Approach #1: Using dyanmic programming.

    class Solution {
    public:
        int maxEnvelopes(vector<pair<int, int>>& envelopes) {
            if (envelopes.size() == 0) return 0;
            sort(envelopes.begin(), envelopes.end(), cmp);
            int ans = 0;
            vector<int> dp(envelopes.size(), 1);
            for (int i = envelopes.size()-1; i >= 0; --i) {
                //std::cout << envelopes[i].first << " " << envelopes[i].second << endl;
                for (int j = i+1; j < envelopes.size(); ++j) {
                    if (envelopes[i].first < envelopes[j].first && envelopes[i].second < envelopes[j].second) {
                        dp[i] = max(dp[i], dp[j]+1);
    
                    }
                }
                //std::cout << dp[i] << endl;
                ans = max(ans, dp[i]);
            }
            return ans;
        }
        static bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
            if (a.first == b.first) return a.second < b.second;
            else return a.first < b.first;
        }
    };
    
    Runtime: 360 ms, faster than 8.88% of C++ online submissions for Russian Doll Envelopes.

    Approach #2: Have the samiler way with 300. Longest Increasing Subsequence

     
    class Solution {
    public:
        int maxEnvelopes(vector<pair<int, int>>& envelopes) {
            if (envelopes.size() == 0) return 0;
            sort(envelopes.begin(), envelopes.end(), cmp);
            int ans = 0;
            vector<int> dp;
            
            for (auto envelope : envelopes) {
                auto it = lower_bound(dp.begin(), dp.end(), envelope.second);
                if (it == dp.end()) dp.push_back(envelope.second);
                else if (*it > envelope.second) *it = envelope.second;
            }
    
            return dp.size();
        }
        static bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
            if (a.first == b.first) return a.second > b.second;
            else return a.first < b.first;
        }
    };
    

    Runtime: 16 ms, faster than 99.58% of C++ online submissions for Russian Doll Envelopes.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    不用再去找rem了,你想要的rem都在这
    linux下ftp配置文件详解
    Linux chmod命令修改文件与文件夹权限命令代码
    如何在linux下开启FTP服务
    解决ftp客户端连接验证报错Server sent passive reply with unroutable address. Using server address instead
    预定义编译器宏
    类的成员变量修饰 const 和static
    【转】svn http://提示svn: Unrecognized URL scheme错误
    EVEREST Ultimate Edition 5.50 正式版 序列号
    [转]Linux下查看文件和文件夹大小
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9903328.html
Copyright © 2011-2022 走看看