Sliding Window Maximum 解答

Question

Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving.

Example

For array [1, 2, 7, 7, 8], moving window size k = 3. return [7, 7, 8]

At first the window is at the start of the array like this

[|1, 2, 7| ,7, 8] , return the maximum 7;

then the window move one step forward.

[1, |2, 7 ,7|, 8], return the maximum 7;

then the window move one step forward again.

[1, 2, |7, 7, 8|], return the maximum 8;

Challenge

o(n) time and O(k) memory

Solution

Key to the solution is to maintain a deque (size <= k) whose elements are always in descending order. Then, the first element is what we want.

Every time we want to add a new element, we need to check:

1. whether it is bigger than previous elements in deque.

If yes, we remove elements in deque which are smaller than current element.

2. whether the first element in deque is out of current sliding window.

If yes, we remove first element.

 1 public class Solution {
 2
 3     public ArrayList<Integer> maxSlidingWindow(int[] nums, int k) {
 4         ArrayList<Integer> result = new ArrayList<Integer>();
 5         Deque<Integer> deque = new LinkedList<Integer>();
 6         int i = 0;
 7         for (int current : nums) {
 8             i++;
 9             // Ensure current deque is in decending order
10             while (!deque.isEmpty() && deque.peekLast() < current)
11                 deque.pollLast();
12             deque.addLast(current);
13             if (i > k && deque.peekFirst() == nums[i - k - 1])
14                 deque.pollFirst();
15             if (i >= k)
16                 result.add(deque.peekFirst());
17         }
18         return result;
19     }
20 }
时间: 2024-10-06 00:07:35

Sliding Window Maximum 解答的相关文章

[LeetCode][JavaScript]Sliding Window Maximum

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.

【LeetCode】239. Sliding Window Maximum

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.

leetcode 239. Sliding Window Maximum

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 posi

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

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 num

LeetCode -- 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,Give

239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

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 knumbers in the window. Each time the sliding window moves right by one position. For example,Given nums 

[leedcode 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

【Leetcode】Sliding Window Maximum

题目链接:https://leetcode.com/problems/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 sli