Sort by

recency

|

1473 Discussions

|

  • + 0 comments
    vector<int> getMax(vector<string> operations) {
        vector<int> ans;
        int Max = INT_MIN;
        stack<int> st;
        int n=operations.size();
        for(int i=0;i<n;i++){
            string s = operations[i];
            int opern = stoi(s.substr(0,1));
            
            //perform operations
            if(opern == 1){ //push operation
                int val = stoi(s.substr(2));
                if(val>=Max){
                    st.push(Max);
                    Max=val;
                }
                st.push(val);
            }
            else if(opern == 2){ // pop operation
                if(Max == st.top()){
                    st.pop();
                    Max = st.top();
                }
                st.pop();
            }
            else if(opern == 3){
                ans.push_back(Max);
            }
        }
        return ans;
    }
    
  • + 0 comments

    C++ Solution:

    vector<int> getMax(vector<string> operations) {
        stack<int> s;
        stack<int> maxStack;    
        vector<int> results;
        for(auto item : operations){
            string op;
            string val;
            stringstream ss(item);
            ss >> op >> val;
            switch (stoi(op)) {
                case 1:
                    s.push(stoi(val));
                    if(!maxStack.empty() && maxStack.top()<=stoi(val))
                        maxStack.push(stoi(val));
                    if(maxStack.empty())
                        maxStack.push(stoi(val));
                    break;
                case 2: 
                    if(!maxStack.empty() && !s.empty() && maxStack.top() == s.top())
                        maxStack.pop();
                    if(!s.empty())
                        s.pop();
                    break;
                case 3: 
                    if(!maxStack.empty())
                        results.push_back(maxStack.top());
                    else results.push_back(0);
                    break;
            }
        }
        return results;
    }
    
  • + 0 comments

    Hi, I'm using this: function getMax(operations) { const operationsToDo = operations.slice(1); let result = []; let temp = [];

    for (let op of operationsToDo) {
        const [operation, value] = op.split(' ').map(Number);
        if(operation === 1){
            temp.push(value);
        } else if(operation === 2){
            temp.pop();
        } else if(operation === 3 && temp.length > 0) {
            result.push(Math.max(...temp));
        }
    };   
    
    return result;
    

    I get an error, do you know why }?

  • + 0 comments

    include

    KAIF BINA CHEATING K KARO

  • + 1 comment

    c++ vector getMax(vector operations) { vector result; stack stk; stack maxStack;

    for (string op : operations) {
        stringstream ss(op);
        int command, value;
        ss >> command;
    
        if (command == 1) {
            ss >> value;
            stk.push(value);
            if (maxStack.empty() || value >= maxStack.top()) {
                maxStack.push(value);
            }
        } else if (command == 2) {
            if (!stk.empty()) {
                if (stk.top() == maxStack.top()) {
                    maxStack.pop();
                }
                stk.pop();
            }
        } else if (command == 3) {
            if (!maxStack.empty()) {
                result.push_back(maxStack.top());
            }
        }
    }
    
    return result;
    

    }