Skip to content

Instantly share code, notes, and snippets.

@shharma-vipin
Created August 9, 2019 14:08
Show Gist options
  • Save shharma-vipin/2532c814e9b999d5788b7e1697fda7c6 to your computer and use it in GitHub Desktop.
Save shharma-vipin/2532c814e9b999d5788b7e1697fda7c6 to your computer and use it in GitHub Desktop.
Slidding Window Problem- Find max number in each window of size K
public List<Integer> findMaxNumberInGivenSubArray(List<Intger> inputArray, int k){
Deque<Integer> deque = new LinkedList<>();
List<Integer> ouputArray = new ArrayList<>();
int i =0;
for(; i < k; i++){
while(!deque.isEmpty() && inputArray.get(deque.peekLast()) < inputArray.get(i)){
deque.removeLast();
}
dequeu.putLast(i);
}
for(; i < inputArray.size(); i++){
outputArray.add(deque.peekFirst());
while(!deque.isEmpty() && deque.peekFirst() <= i-k){
deque.removeFirt();
}
while(!deque.isEmpty() && inputArray.get(deque.peekLast()) < inputArray.get(i)){
deque.removeLast();
}
deque.addLast(i);
}
return ouputArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment