Breaking News: Grepper is joining You.com.
Check it out

# Feature #1: Select Closest Drivers

## Feature #1: Select Closest Drivers

0

Solution

The Euclidean distance between a point P(x,y) and the origin can be calculated using the following formula:

​​Now that you can calculate the distance between a user and all nearby drivers, how will you find the K nearest drivers? The best data structure that comes to mind to track the nearest K drivers is Heap.

We iterate through the array and calculate the distance between each driver’s current location and the user. We’ll insert the distances of the first K drivers into the Heap. Each time we find a distance smaller than the maximum distance in the Heap, we do two things:

Remove the maximum distance from the Heap

Insert the smaller distance into the Heap

This will ensure that we always have K minimum distances in the Heap. The most efficient way to repeatedly find the maximum number among a set of numbers is to use a max-Heap.

Below is an illustration of this process. We have mapped the city of Seattle onto the cartesian plane to get simpler latitude and longitude values for the driver’s location.

Popularity 1/10 Helpfulness 1/10 Language scala
Source: Grepper
Tags: