diff --git a/java/Stacks/MaximumsOfSlidingWindow.java b/java/Stacks/MaximumsOfSlidingWindow.java index e7ff705..a4aa0e6 100644 --- a/java/Stacks/MaximumsOfSlidingWindow.java +++ b/java/Stacks/MaximumsOfSlidingWindow.java @@ -1,28 +1,29 @@ -import java.util.Stack; +import java.util.ArrayDeque; +import java.util.Deque; public class MaximumsOfSlidingWindow { public int[] maximumsOfSlidingWindow(int[] nums, int k) { int[] res = new int[nums.length - k + 1]; - Stack dq = new Stack<>(); + Deque dq = new ArrayDeque<>(); int left, right; left = right = 0; while (right < nums.length) { // 1) Ensure the values of the deque maintain a monotonic decreasing order // by removing candidates <= the current candidate. - while (!dq.isEmpty() && dq.peek()[0] <= nums[right]) { - dq.pop(); + while (!dq.isEmpty() && dq.peekLast()[0] <= nums[right]) { + dq.pollLast(); } // 2) Add the current candidate. - dq.push(new int[]{nums[right], right}); + dq.offerLast(new int[]{nums[right], right}); // If the window is of length 'k', record the maximum of the window. if (right - left + 1 == k) { // 3) Remove values whose indexes occur outside the window. - if (!dq.isEmpty() && dq.firstElement()[1] < left) { - dq.remove(0); + if (!dq.isEmpty() && dq.peekFirst()[1] < left) { + dq.pollFirst(); } // The maximum value of this window is the leftmost value in the // deque. - res[left] = dq.firstElement()[0]; + res[left] = dq.peekFirst()[0]; // Slide the window by advancing both 'left' and 'right'. The right // pointer always gets advanced so we just need to advance 'left'. left++;