diff --git a/239-Sliding-Window-Maximum/solution.java b/239-Sliding-Window-Maximum/solution.java new file mode 100644 index 0000000..8f6e642 --- /dev/null +++ b/239-Sliding-Window-Maximum/solution.java @@ -0,0 +1,32 @@ +public class Solution { + public void addElement(Deque dq, int element){ + while(!dq.isEmpty() && dq.peekLast() <= element){ + dq.pollLast(); + } + dq.offerLast(element); + } + + public void removeElement(Deque dq, int element){ + if(!dq.isEmpty() && dq.peekFirst() == element){ + dq.pollFirst(); + } + } + public int[] maxSlidingWindow(int[] nums, int k) { + if (nums == null || k <= 0) { + return new int[0]; + } + + Deque dq = new ArrayDeque(); + int length = nums.length; + int[] result = new int[length - k +1]; + for(int i =0; i