If you have a stream of values coming in, and at any given time you may be asked to process the maximum or minimum value, then a heap is what you want. A max-heap is used to extract and process the maximum; a min-heap is used to extract and process the minimum.
If you have a stream of values coming in and at any moment you are asked to take the min or max out and use it for some task heaps will be used.
- Using the max or min?—if you are not processsing the max values you will not need a heap. You can store that max in a varaible and only change it when the coming data is more than what you had uptill that point. The maxes before it are irrelevant. For that heaps will not be used it is when you are using that max value for something where the power of heap can be unleashed.
- Like the most high priority task, after you are done with the task you get rid of that task and the one below it becomes that of the highest priority.
We need the max sale number for the first 3 days.
We can’t sort the whole array as we only need the max from the first three days. So will have to create a seprate array.
We have an array of information and we want the maximum uptil a certain point the heap is our go to, we can localize storage without compromising the whole data.
We need the max for the first 3 days
sale = [1,6,7,3,11,7,8,4,1,3,4,6]