Amazon Interview Question
Country: United States
That would not work,, what if the element is some where between max and min, you cannot just enter it anywhere right?
Max- heap, is said of using priority heap, but complexity for it is nlogn which is very high compared to 0(1), so that too would not work.
I said it wrong, if the new distance is smaller than the max of the 10-element array, which is at one end of the array since the array is kept sorted, simply throw away the max and insert the new distance in, much like in insertion sort. And since the array has up to 10 element, the insertion takes O(10) each time a new distance is inserted or O(1) if larger than the max. As to heap, we also only keep up to 10 elements in the max heap
use an extra 10-element sorted array. every time a new point is added, its distance is compared with the maximum of this array at one end and if larger, remove the largest and insert the new point. Could also use a max-heap.
- pdgetrf March 28, 2012