Sliding Window Maximum @LeetCode
PROBLEM :
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?
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans ;
if(nums.size()==0)
return ans ;
vector<int> left(nums.size()) ;
left[0]=nums[0] ;
for(int i=1;i<nums.size();i++)
left[i]=(i%k==0) ? nums[i] : (nums[i] > left[i-1] ? nums[i] : left[i-1]) ;
vector<int> right(nums.size()) ;
right[nums.size()-1]=nums[nums.size()-1] ;
for(int i=nums.size()-2;i>=0;i--)
right[i]=(i%k==0) ? nums[i] : (nums[i] > right[i+1] ? nums[i] : right[i+1]) ;
for(int i=0;i<nums.size()-k+1;i++)
ans.push_back( right[i]>left[i+k-1] ? right[i] : left[i+k-1] ) ;
return ans ;
}
};
--------------------------------------------------------------------------------
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?
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ans ;
if(nums.size()==0)
return ans ;
vector<int> left(nums.size()) ;
left[0]=nums[0] ;
for(int i=1;i<nums.size();i++)
left[i]=(i%k==0) ? nums[i] : (nums[i] > left[i-1] ? nums[i] : left[i-1]) ;
vector<int> right(nums.size()) ;
right[nums.size()-1]=nums[nums.size()-1] ;
for(int i=nums.size()-2;i>=0;i--)
right[i]=(i%k==0) ? nums[i] : (nums[i] > right[i+1] ? nums[i] : right[i+1]) ;
for(int i=0;i<nums.size()-k+1;i++)
ans.push_back( right[i]>left[i+k-1] ? right[i] : left[i+k-1] ) ;
return ans ;
}
};
--------------------------------------------------------------------------------
Comments
Post a Comment