问题
Ten million elements are entered into an array (no memory constraints). As we know, while entering the elements we can update the max out of entered values by a check whenever we enter a value.
But imagine if the position of max value is somewhere around 9 million
If I remove 2 million elements in positions 8 to 10 million without doing any more comparisons, we should have the next maximum value. Will that mean while entering the data we should have a plan to organize the data in some way to get the max value out of the remaining data?
Deleting and inserting will keep on happening, but we should have the new/residual maximum value updated in less time with a smaller number of steps. (Using multiple stacks might help.)
回答1:
For that you can also do by inserting the values in the array in the sorted order,means checking the right place of the inserted element in the sorted form.
回答2:
If block deletions are a common operation, you could maintain a hierarchy of maxima of spans of the list. For each edit you then have to update that data, but that's something like O(log n) rather than O(m log n) if you simply iterated through the list of m deletions removing them one-by-one from the heap.
来源:https://stackoverflow.com/questions/30689110/finding-maximum-value