# Hard-题目15：239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       3
1 [3  -1  -3] 5  3  6  7       3
1  3 [-1  -3  5] 3  6  7       5
1  3  -1 [-3  5  3] 6  7       5
1  3  -1  -3 [5  3  6] 7       6
1  3  -1  -3  5 [3  6  7]      7

Therefore, return the max sliding window as [3,3,5,5,6,7].

public class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length==0)
return new int[0];
int[] window = new int[nums.length-k+1];
int j=0;
for(int i = 0;i<nums.length;i++) {
while (!deque.isEmpty() && nums[deque.getLast()] < nums[i]) {
deque.removeLast();
}
if(deque.getFirst() == i-k)
deque.removeFirst();
if(i>=k-1)
window[j++] = nums[deque.getFirst()];
}
return window;
}
}

29ms,beats 62.07%,众数31ms,9.54%
Cmershen的碎碎念：

#### LeetCode 239. Sliding Window Maximum（滑动窗口最大值）

2016-04-06 12:53:10

#### leetcode 239: Sliding Window Maximum

2015-07-19 15:41:45

#### [LeetCode]239.Sliding Window Maximum

2015-07-19 15:49:54

#### 239. Sliding Window Maximum

2016-05-15 15:26:46

#### [LeetCode]239. Sliding Window Maximum

2017-03-11 22:17:33

#### [leetcode] 239. Sliding Window Maximum

2016-05-16 21:35:05

#### Leetcode 239. Sliding Window Maximum

2016-09-04 01:29:21

#### leetcode 239. Sliding Window Maximum

2016-03-04 13:28:44

#### [leetcode]239. Sliding Window Maximum

2017-07-04 15:23:23

#### LeetCode 239. Sliding Window Maximum

2016-04-27 00:31:11