LeetCode -- Sliding Window Maximum

Source: Internet
Author: User

LeetCode -- Sliding Window Maximum
Description:


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


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


In an array, there is a window with a length of k. Every time the window is moved back to one unit, the maximum value is taken out and put into the list until the right end of the window reaches the end of the array, returns the list of the largest arrays.


Ideas:
1. In this question, we need to use a list to express the window, remove it in the first place, and add it at the end.
2. to optimize performance, you can maintain a maxIndex variable to reduce the maximum number of computations. When the window moves, if index indicates the end position of the window, then:
A. If maxIndex is removed first, recalculate the maximum value and update maxIndex.
B. If maxIndex is not removed first, you only need to compare nums [maxIndex] And nums [index] to update maxIndex.




Implementation Code:



public class Solution {    public int[] MaxSlidingWindow(int[] nums, int k) {        if(nums.Length == 0){    return new int[0];    }    if(k > nums.Length - 1){    return new int[]{nums.Max()};    }          var result = new List
 
  ();     var window = new List
  
   ();     var maxIndex = 0;     for(var i = 0;i < k; i++){     window.Add(nums[i]);    if(nums[maxIndex] < nums[i]){    maxIndex = i;    }     }     result.Add(nums[maxIndex]);          var right = k;     while(right < nums.Length){     window.RemoveAt(0);    window.Add(nums[right]);        if(right - k == maxIndex){    maxIndex = CalcMaxIndex(right-k+1, k, nums);    }else{    if(nums[right] > nums[maxIndex]){    maxIndex = right;    }    }    result.Add(nums[maxIndex]);    right ++;     }          return result.ToArray();}private int CalcMaxIndex(int left, int k, int[] nums){var maxIndex = left ;for(var j = left ;j < left + k; j++){if(nums[j] > nums[maxIndex]){maxIndex = j;}}return maxIndex;}    }
  
 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.