Sort by

recency

|

1470 Discussions

|

  • + 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;
    

    }

  • + 0 comments

    I am not sure why this is under stack. Using stack won't pass the runtime requirement. Getting the max value using Stack takes more time than the List.

  • + 0 comments

    Python

    Solution

    def getMax(operations):

    stack = list()
    sol = list()
    max_number = [0]
    
    for i in operations:
        if i[0]=='1':
            number = int(i[2:])
            stack.append(number)
            if number >= max_number[-1]:
                max_number.append(number)
    
        elif i[0]=='2' and stack:
            poppednumber = stack.pop()
            if poppednumber==max_number[-1]:
                max_number.pop()
    
        elif i[0]=='3':
            sol.append(max_number[-1])
    
    return sol
    
  • + 0 comments

    Why programmer using C, uploaded code and did not use the fonction in the online editor on the site