forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
campus-bikes.py
35 lines (30 loc) · 1.09 KB
/
campus-bikes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Time: O((w * b) * log(w * b))
# Space: O(w * b)
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(p1, p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
distances = [[] for _ in xrange(len(workers))]
for i in xrange(len(workers)):
for j in xrange(len(bikes)):
distances[i].append((manhattan(workers[i], bikes[j]), i, j))
distances[i].sort(reverse = True)
result = [None] * len(workers)
lookup = set()
min_heap = []
for i in xrange(len(workers)):
heapq.heappush(min_heap, distances[i].pop())
while len(lookup) < len(workers):
_, worker, bike = heapq.heappop(min_heap)
if bike not in lookup:
result[worker] = bike
lookup.add(bike)
else:
heapq.heappush(min_heap, distances[worker].pop())
return result