Skip to content

Instantly share code, notes, and snippets.

@chaudharisuresh997
Created November 25, 2019 04:43
Show Gist options
  • Save chaudharisuresh997/a6468367d6c64ff49b99d90426a3078e to your computer and use it in GitHub Desktop.
Save chaudharisuresh997/a6468367d6c64ff49b99d90426a3078e to your computer and use it in GitHub Desktop.
Sliding Window Maximum-Partially accpeted
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