打卡(9)

能做的就是体会一下这个类的写法吧,反正其实也没什么用处。用两个栈来实现一个队列。

class MyQueue {
    
    
private:
    stack<int> inStack, outStack;
    void in2out() {
    
    
        while (!inStack.empty()) {
    
    
            outStack.push(inStack.top());
            inStack.pop();
        }
    }
public:
    //体会一下这个类的写法
    MyQueue() {
    
    }
    void push(int x) {
    
    
        inStack.push(x);
    }
    int pop() {
    
    
        if (outStack.empty()) {
    
    
            in2out();
        }
        int x = outStack.top();
        outStack.pop();
        return x;
    }
    int peek() {
    
    
        if (outStack.empty()) {
    
    
            in2out();
        }
        return outStack.top();
    }
    bool empty() {
    
    
        return inStack.empty() && outStack.empty();
    }
};
class Solution {
    
    
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
    
    
        int n = envelopes.size();
        if(n <= 1)
            return n;

        sort(envelopes.begin(), envelopes.end());  

        vector<int> dp(n, 1);

        for(int i = 0; i < n; i++)
            for(int j = 0; j < i; j++)
                if((envelopes[j][0] < envelopes[i][0]) && (envelopes[j][1] < envelopes[i][1]))
                    dp[i] = max(dp[i], dp[j] + 1);

        return *max_element(dp.begin(), dp.end());
    }
};

作者:starfirz
链接:https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-di-zeng-zi-xu-lie-de-er-wei-bi-i9na/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

第二个先码住,以后好好看看。

猜你喜欢

转载自blog.csdn.net/weixin_47741017/article/details/114407994