Created
August 9, 2019 14:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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