You are viewing a single comment's thread. Return to all comments →
Java 8 Solution
public static List<Integer> getMax(List<String> operations) { Stack<Integer> stack = new Stack<>(); List<Integer> ans = new ArrayList<>(); for (String operation: operations) { Integer cmd = Integer.parseInt(operation.split(" ")[0]); if (cmd.equals(1)) { Integer value = Integer.parseInt(operation.split(" ")[1]); stack.push(value); } else if (cmd.equals(2) && !stack.empty()) { stack.pop(); } else { Integer max = stack.stream().mapToInt(i -> i).max().getAsInt(); ans.add(max); } } return ans; }
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 →
Java 8 Solution