码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Sliding Window Maximum

时间:2015-07-18 22:42:08      阅读:555      评论:0      收藏:0      [点我收藏+]

标签:

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].

Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array‘s size for non-empty array.

Follow up:
Could you solve it in linear time?

Hint:

  1. How about using a data structure such as deque (double-ended queue)?
  2. The queue size need not be the same as the window’s size.
  3. Remove redundant elements and the queue should store only elements that need to be considered

https://leetcode.com/problems/sliding-window-maximum/

 

 

 


 

 

首先是非线性复杂度的解法,直接用js的数组来模拟双向队列。

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @return {number[]}
 5  */
 6 var maxSlidingWindow = function(nums, k) {
 7     var window = [], result = [];
 8     for(var i = 0; i < nums.length; i++){
 9         if(window.length === k){
10             result.push(findMax(window));
11             window.shift();
12         }
13         window.push(nums[i]);
14     }
15     if(window.length === k){
16         result.push(findMax(window));
17     }
18     return k !== 0 ? result : [];
19 
20     function findMax(arr){
21         var max = -Infinity;
22         for(var i = 0; i < arr.length; i++){
23             if(arr[i] > max){
24                 max = arr[i];
25             }
26         }
27         return max;
28     }
29 };

 

线性复杂度的解法。

https://leetcode.com/discuss/46578/java-o-n-solution-using-deque-with-explanationa

核心思想就是在队列头保存最大元素的下标。

每一轮循环:

1.先移除过期的元素;

2.从后往前移出不可能是结果的元素,两种情况:

i)比如队列是[2,3],这一轮的元素是4,那么max一定是4,2和3都可以移除了;

ii)比如队列是[5,3],这一轮的元素是4,那么要移出3,最后的结果应该是[5,4]。

因为等5过期了,4可能就是最大的元素了,所以要放着。为什么可以放心地移除3呢,因为4的过期时间肯定比3久,而且大于3。 <-- 此处是难点

3.往队列里放入这一轮的元素,如果长度大于等于窗口长度与就输出。

保存下标而非值是为了之后可以方便地找出过期的元素,需要值的时候直接可以通过下标访问nums。

 

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @return {number[]}
 5  */
 6 var maxSlidingWindow = function(nums, k) {
 7     var window = [], index = -1, result = [];
 8     for(var i = 0; i < nums.length; i++){
 9         while(window.length !== 0 && window[0] < i - k + 1){
10             window.shift();
11         }
12         while(window.length !== 0 && nums[window[window.length - 1]] < nums[i]){
13             window.pop();
14         }
15         window.push(i);
16         if(i >= k - 1){
17             result.push(nums[window[0]]);
18         }
19     }
20     return result;   
21 };

 

 

 

 

 

[LeetCode][JavaScript]Sliding Window Maximum

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4657526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!