Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- class Solution(object):
- def assignBikes(self, workers, bikes):
- """
- :type workers: List[List[int]]
- :type bikes: List[List[int]]
- :rtype: List[int]
- """
- def manhattan_distance(p1_x, p1_y, p2_x, p2_y):
- return abs(p1_x - p2_x) + abs(p1_y - p2_y)
- results = [-1] * len(workers)
- computations = []
- assigned_bikes = set()
- assigned_workers = set()
- for worker_idx in range(len(workers)):
- for bike_idx in range(len(bikes)):
- p1_x, p1_y = workers[worker_idx]
- p2_x, p2_y = bikes[bike_idx]
- dist = manhattan_distance(p1_x, p1_y, p2_x, p2_y)
- computations.append((dist, worker_idx, bike_idx))
- sorted_list = sorted(computations, key=lambda x: (x[0], x[1], x[2]))
- for tu in sorted_list:
- _, worker_idx, bike_idx = tu
- if worker_idx not in assigned_workers and bike_idx not in assigned_bikes:
- assigned_workers.add(worker_idx)
- assigned_bikes.add(bike_idx)
- results[worker_idx] = bike_idx
- return results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement