Prateek
BAN USER
// from an array build max Heap
// here array is A, mSize is size of array
// and finally return the top element of the heap by taking out k-1 max elements out of heap
// Main Program
MyHeap maxHeap = new MyHeap(A);
maxHeap.buildHeap();
for(int i=0;i<k-1;i++)
maxHeap.removeTop();
maxHeap.getPriorityElement(); // required result
public MyHeap(ArrayList<Integer> arr){
mArrList = arr;
mSize = arr.size();
}
public void buildHeap(){ O(n)
for( int i=mSize/2-1; i>=0; i-- ){
max_heapify(i);
}
}
private boolean max_heapify(int pos){
int sPos = getMaxOfTwoChild(pos);
if( pos > mSize || sPos == -1 )
return true;
if( mArrList.get(pos-1) < mArrList.get(sPos-1) ){
swap(pos-1,sPos-1);
return max_heapify(sPos);
}
else
return true;
}
private int getMaxOfTwoChild(int pos) {
int left = leftOf(pos);
int right = rightOf(pos);
if( left <= mSize && right <= mSize && left > 0 ){
return mArrList.get(left-1) > mArrList.get(right-1) ? left : right;
}
else if( left <= mSize && right > mSize )
return left;
else if ( right <= mSize && left > mSize )
return right;
return -1;
}
public int removeTop(){ // O(k * logn)
int top = getPriorityElement();
if( top != -1 ){
swap(0,mSize-1);
mArrList.remove(--mSize);
max_heapify(1);
}
return top;
}
public int getPriorityElement(){
if( mSize > 0 )
return mArrList.get(0);
else return -1;
}
private int leftOf(int pos){
return 2*pos;
}
private int rightOf(int pos){
return 2*pos+1;
}
// total complexity of program = O(n) + O(k * logn )
- Prateek March 04, 2012
Repjoyjerraj, Android test engineer at ASAPInfosystemsPvtLtd
I am a sales manager .I'm responsible for leading and coaching a team of salespeople. A sales manager's ...
Replillyalaird, Associate at Achieve Internet
I am Lilly from Eau Claire USA, I am working as a manager in a Best products company. My interest ...
Repjbacklit, abc at 247quickbookshelp
I am working as a news anchor and i love my job i an working from last 4 years under ...
Repruchikadolph, HR Executive at Accenture
I choose the talking books to match the book library clerk blind library patrons . I compare a list of borrowers ...
Repjuanitajboon, Applications Developer at 247quickbookshelp
Hi everyone, I am from Pelham. I currently work in the Hechinger as Cashier.I like to do creative things ...
Repdalejohnson762, Accountant at ABC TECH SUPPORT
I have exceptionally skilled Journalists possessed dogged determination to find the new story and deliver it to the public. I ...
Reparlinenwalters, Software Analyst at ASAPInfosystemsPvtLtd
I Performed extensive web research to collect pertinent data and gather images related to the assigned articleIts act of writing ...
Repjesselblagg9, Cloud Support Associate at 247quickbookshelp
Hello, I am Jesse and I live in Springfield, USA. I am working in a company as an engineering technician ...
Repsylviatobins, Applications Developer at 247quickbookshelp
Hi, I am Sylvia Law librarian. I am an information resource expert. I work in law schools, corporate law departments ...
Reppamilarbowman, Animator at ABC TECH SUPPORT
Je travaille en tant que Web Manager dans la société Forum Cafeterias. Je veux tout savoir sur le marketing numérique ...
- Prateek April 19, 2012