Flashcards
Find Maximum in Sliding Window
Cards in this deck (1)
- 1Find Maximum in Sliding Windowclass Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0 || k <= 0) return nums;
int r = 0;
int[] slide = new int[nums.length - k + 1];
Deque q = new ArrayDeque<>();
for(int i = 0; i< nums.length; i++){
while(!q.isEmpty() && q.peek() < i - k + 1){
q.poll();
}
while(!q.isEmpty() && nums[q.peekLast()] < nums[i]){
q.pollLast();
}
q.offer(i);
if(i >= k - 1){
slide[r++] = nums[q.peek()];
}
}
return slide;
}
}
Information
- Description
- Given an array of integers, rotate the array by 'N' elements.
- Created by
- Suhas S
- Views
- 13
- Tags
- Rating