bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1057. Campus Bikes

1057. Campus Bikes

Leetcode Solutions

Bucket Sort Approach

  1. Initialize an array of lists, buckets, where each list corresponds to a possible Manhattan distance from 0 to 1998.
  2. For each worker, calculate the Manhattan distance to each bike and add the (worker index, bike index) pair to the corresponding bucket.
  3. Initialize an array, answer, to store the bike assigned to each worker, and a set, assignedBikes, to keep track of which bikes have been assigned.
  4. Iterate over the buckets array from the smallest distance to the largest. a. For each bucket, iterate over the (worker, bike) pairs. b. If the worker has not been assigned a bike and the bike has not been assigned, assign the bike to the worker, add the bike to assignedBikes, and update answer.
  5. Return the answer array.
UML Thumbnail

Sorting Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...