From e3ad629d0b7b541fd87f180899dda625e70b87bd Mon Sep 17 00:00:00 2001 From: seaskymonster Date: Sat, 1 Oct 2016 01:06:19 -0700 Subject: [PATCH] [239. Sliding Window Maximum][Time Limit Exceeded]committed by LeetCode Extension --- 239-Sliding-Window-Maximum/solution.java | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 239-Sliding-Window-Maximum/solution.java 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