문제

Please see this question and the accepted answer:

Algorithm to calculate number of intersecting discs

Instead of using a binary tree, what if you were to use an array, sort it and then simply iterate up through the array? Would it still be O(n log n)?

Iterating through the sorted array (to me) seems like it would be O(n^2)....

도움이 되었습니까?

해결책

The accepted answer speaks about binary search over a sorted array, not about a binary tree. Binary search is logn, "simply iterating" is n. So you would make the solution O(n^2) indeed. But there is no point in simple iteration over an array that you know is sorted - you can use binary search.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top