Check in (9)

What can be done is to experience the writing of this class, it is actually useless anyway. Two stacks are used to implement a queue.

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());
    }
};

Author: starfirz
link: https: //leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-di-zeng-zi-xu-lie-de-er-wei-bi-i9na/
sources : LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

The second one will live first and take a look at it later.

Guess you like

Origin blog.csdn.net/weixin_47741017/article/details/114407994
9