Sort by

recency

|

1478 Discussions

|

  • + 0 comments

    Took me embarassingly longer than i'd like to admit to get this one. Lots of edge cases have to be accounted for. Used C++ with two stacks, you could also do it with vectors too.

    vector<int> getMax(vector<string> operations) {
        
        vector<int> results;
        std::stack<int> stack;
        std::stack<int> maxStack;
        
        for (int i = 0; i < operations.size(); ++i) {
            int op = operations.at(i).at(0) - '0';
            
            if (op == 1) {
                int val = stoi(operations.at(i).substr(2));
                stack.push(val);
                
                if (maxStack.empty() || (!maxStack.empty() && val >= maxStack.top())) {
                    maxStack.push(val);
                }
            } else if (op == 2) {
                if (!maxStack.empty() && stack.top() == maxStack.top())
                    maxStack.pop();
                    
                stack.pop();
            } else if (op == 3) {
                if (!maxStack.empty())
                    results.push_back(maxStack.top());
            }
        }
        
        return results;
    }
    
  • + 0 comments

    En Python

    def getMax(operations): s = [] p = [] for i in range(n): s.append(str(ops[i]).split(' ')) if s[i][0] == '1': p.append(int(s[i][1])) if s[i][0] == '2': p.pop() if s[i][0] == '3': print(max(p))

  • + 0 comments

    My C code int* getMax(int operations_count, char** operations, int* result_count) { int* stack = malloc(operations_count * sizeof(int)); int stack_size = 0; int* result = malloc(operations_count * sizeof(int)); *result_count = 0;

    for (int i = 0; i < operations_count; i++) {
        if (strncmp(operations[i], "1 ", 2) == 0) {
            int val;
            sscanf(operations[i], "1 %d", &val);
            stack[stack_size++] = val;
        } else if (strncmp(operations[i], "2", 1) == 0) {
            if (stack_size > 0) stack_size--;
        } else if (strncmp(operations[i], "3", 1) == 0) {
            if (stack_size > 0) {
                result[*result_count] = stack[0];
                for (int j = 1; j < stack_size; j++) {
                    if (stack[j] > result[*result_count]) {
                        result[*result_count] = stack[j];
                    }
                }
                (*result_count)++;
            }
        }
    }
    
    free(stack);
    return realloc(result, *result_count * sizeof(int));
    

    }

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