Sort by

recency

|

1475 Discussions

|

  • + 0 comments

    Python

    def equalStacks(h1, h2, h3):
        # Write your code here
        sum1=sum(h1)
        sum2=sum(h2)
        sum3=sum(h3)
        if sum1==sum2==sum3:
            return sum1
        else:
            index1,index2,index3=0,0,0
            while not sum1==sum2==sum3:
                if sum1 > sum2 or sum1>sum3:
                    sum1-=h1[index1]
                    index1+=1
                elif sum2 > sum1 or sum2>sum3:
                    sum2-=h2[index2]
                    index2+=1
                elif sum3 > sum1 or sum3>sum2:
                    sum3-=h3[index3]
                    index3+=1
            if index1==len(h1) or index2==len(h2) or index3==len(h3):
                return 0
            else:
                return sum1
    
  • + 0 comments
        public static List<Integer> getMax(List<String> operations) {
            Stack<Integer> queue = new Stack<Integer>();
            List<Integer> result = new ArrayList<Integer>();
            PriorityQueue<Integer> sortedNumbers = new PriorityQueue<Integer>(Collections.reverseOrder());
            
            for (String operation: operations) {
                String[] partes = operation.split(" ");            
                
                if ("1".equals(partes[0])) {
                    queue.push(Integer.parseInt(partes[1]));
                    sortedNumbers.add(Integer.parseInt(partes[1]));
                }
                
                if ("2".equals(partes[0])) {
                    Integer removido = queue.pop();
                    sortedNumbers.remove(removido);
                }
                
                if ("3".equals(partes[0])) {                
                    result.add(sortedNumbers.peek());
                }            
                
            }        
            
            return result;
        }
    
  • + 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 }?