Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Feature #1: Select Closest Drivers

Sumit Rawal answered on May 24, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


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
Link to this answer
Share Copy Link
Contributed on May 24 2023
Sumit Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.