-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_1057_CampusBikes.py
More file actions
24 lines (21 loc) · 948 Bytes
/
_1057_CampusBikes.py
File metadata and controls
24 lines (21 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#-----------------------------------------------------------------------------
# Runtime: 724ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
import collections
class Solution:
def assignBikes(self, workers: [[int]], bikes: [[int]]) -> [int]:
distances = collections.defaultdict(list)
for i, (worker_x, worker_y) in enumerate(workers):
for j, (bike_x, bike_y) in enumerate(bikes):
distance = abs(worker_x - bike_x) + abs(worker_y - bike_y)
distances[distance].append((i, j))
usedBike = set()
result = [ -1 ] * len(workers)
for key in sorted(distances.keys()):
for worker_id, bike_id in distances[key]:
if result[worker_id] == -1 and bike_id not in usedBike:
result[worker_id] = bike_id
usedBike.add(bike_id)
return result