Created
November 25, 2019 04:43
-
-
Save chaudharisuresh997/a6468367d6c64ff49b99d90426a3078e to your computer and use it in GitHub Desktop.
Sliding Window Maximum-Partially accpeted
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 class SlidingMaximum { | |
public ArrayList<Integer> slidingMaximum(final List<Integer> A,int B){ | |
int start=0; | |
ArrayList<Integer> result=new ArrayList<Integer>(); | |
PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); | |
for(int i=0;i<A.size();i++){ | |
while((i-start)!=(B-1)){ | |
pq.add(A.get(i)); | |
i++; | |
} | |
pq.add(A.get(i));//add current | |
result.add(pq.peek());//mark maximum in result queue | |
//remove expired from heap. | |
if(pq.peek()!=A.get(start)){ | |
pq.remove(A.get(start)); | |
}else{ | |
pq.poll(); | |
} | |
start++; | |
// i=end; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment