Advertisement
Guest User

Untitled

a guest
Aug 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import heapq
  2.  
  3. class Solution(object):
  4. def assignBikes(self, workers, bikes):
  5. """
  6. :type workers: List[List[int]]
  7. :type bikes: List[List[int]]
  8. :rtype: List[int]
  9. """
  10. def manhattan_distance(p1_x, p1_y, p2_x, p2_y):
  11. return abs(p1_x - p2_x) + abs(p1_y - p2_y)
  12.  
  13. results = [-1] * len(workers)
  14. computations = []
  15. assigned_bikes = set()
  16. assigned_workers = set()
  17.  
  18. for worker_idx in range(len(workers)):
  19. for bike_idx in range(len(bikes)):
  20. p1_x, p1_y = workers[worker_idx]
  21. p2_x, p2_y = bikes[bike_idx]
  22. dist = manhattan_distance(p1_x, p1_y, p2_x, p2_y)
  23.  
  24. computations.append((dist, worker_idx, bike_idx))
  25.  
  26. sorted_list = sorted(computations, key=lambda x: (x[0], x[1], x[2]))
  27.  
  28. for tu in sorted_list:
  29. _, worker_idx, bike_idx = tu
  30.  
  31. if worker_idx not in assigned_workers and bike_idx not in assigned_bikes:
  32. assigned_workers.add(worker_idx)
  33. assigned_bikes.add(bike_idx)
  34. results[worker_idx] = bike_idx
  35.  
  36. return results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement