Back to course home
0% completed
Vote For New Content
// if heap has more than 'k' numbers, remove one number if (this.minHeap....
First
Nov 30, 2022
// if heap has more than 'k' numbers, remove one number if (this.minHeap.length > this.k) { this.minHeap.pop(); } How does this loop enough times to pop the minHeap if this is an if statement?
0
0
Comments
Comments
S
Sonia 3 years ago
this should be a while loop instead of an if statement. So while the minHeap size is bigger than k, remove from minHeap.
Design Gurus3 years ago
There is no need for the 'while loop'.
In the 'add' function, we are adding only one number, so the the size of the heap will be incremented by one only. So at max, we have to get only one element out, to bring the heap size under 'k'.
On this page