educative.io

Educative

Can't we achieve the same by getting a list of distances and then sorting?

Iterating through the list to create a map structure I o(n).

Then we sort it in o(nlogn)

After that we can simply pick the top k elements.

Total complexity o(nlogn)