You are viewing a single comment's thread. Return to all comments →
public static List getMax(List operations) { // Write your code here List list = new ArrayList<>();
Stack<Integer> stack = new Stack<>(); Stack<Integer> maxStack = new Stack<>(); // Base conditions are if (operations.size() == 0) { return list; } for (int i =0 ; i < operations.size(); i++) { if(operations.get(i).isEmpty()) continue; String[] arr = operations.get(i).split(" "); int operationType = Integer.valueOf(arr[0]); if (operationType == 1) { int value = Integer.valueOf(arr[1]); stack.push(value); if (maxStack.isEmpty() || maxStack.peek() <= value) { maxStack.push(value); } } else if (operationType == 2) { if (!stack.isEmpty()) { int popedElement = stack.pop(); if (!maxStack.isEmpty() && popedElement == maxStack.peek()) { maxStack.pop(); } } } else if (operationType == 3) { if (!stack.isEmpty()) { list.add(maxStack.peek()); } } } return list;
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
public static List getMax(List operations) { // Write your code here List list = new ArrayList<>();