Finding maximum value [duplicate]

大憨熊 提交于 2019-12-14 00:10:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!