sliding window

A long array A[] is given to you. There is a sliding window of size  w  which is moving from the very left of the array to the very right. You can only see the  w  numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:

The array is [1 3 -1 -3 5 3 6 7], and w is 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
Input : A long array A[], and a window width w
Output : An array B[], B[i] is the maximum value of from A[i] to A[i+w-1]
Requirement : Find a good optimal way to get B[i]


The double-ended queue is the perfect data structure for this problem. It supports insertion/deletion from the front and back. The trick is to find a way such that the largest element in the window would always appear in the front of the queue. How would you maintain this requirement as you push and pop elements in and out of the queue?

Besides, you might notice that there are some redundant elements in the queue that we shouldn’t even consider about. For example, if the current queue has the elements: [10 5 3], and a new element in the window has the element 11. Now, we could have emptied the queue without considering elements 10, 5, and 3, and insert only element 11 into the queue.

A natural way most people would think is to try to maintain the queue size the same as the window’s size. Try to break away from this thought and try to think outside of the box. Removing redundant elements and storing only elements that need to be considered in the queue is the key to achieve the efficient O(n) solution below.


import java.io.*;
import java.util.*;
import java.util.Map.Entry;

/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

public class Solution {
    

  public static int[] slidingWindowMax(int[] nums, int k) 
  {
      int n = nums.length - k + 1;
      Deque<Integer> queue = new ArrayDeque<Integer>();
    
      int[] max = new int[n];
    for (int i = 0; i < nums.length; i++) {
        while(!queue.isEmpty() && nums[queue.getLast()] <= nums[i]) {
           queue.removeLast();
        }
      
      queue.addLast(i);
      if (i < k - 1) continue;
      while(!queue.isEmpty() && i - k >= queue.getFirst())
        queue.removeFirst();
      
      max[i - k + 1] = nums[queue.getFirst()];
    }
    
    return max;
  }

 
  
  public static void main(String[] args) {
          int[] nums = {1,3,-1,-3,5,3,6};
          int[] ret = slidingWindowMax(nums, 3);
          for (int i : ret) {
              System.out.println(i);
          }
    
  }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值