class Solution { public int maxChunksToSorted(int[] arr) { Deque<Integer> stack = new ArrayDeque<>(); for (int x : arr) { if (!stack.isEmpty() && x < stack.peek()) { int max = stack.poll(); while (!stack.isEmpty() && x < stack.peek()) stack.poll(); stack.push(max); } else { stack.push(x); } } return stack.size(); } }
|