-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtaskAssignment.py
25 lines (22 loc) · 970 Bytes
/
taskAssignment.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
def taskAssignment(k, tasks):
pairedTasks = []
taskDurationsToIndices = getTaskDurationsToIndices(tasks)
sortedTasks = sorted(tasks)
for idx in range(k):
task1Duration = sortedTasks[idx]
indicesWithTaskDuration = taskDurationsToIndices[task1Duration]
task1Index = indicesWithTaskDuration.pop()
task2SortedIndex = len(tasks)-1-idx
task2Duration = sortedTasks[task2SortedIndex]
indicesWithTask2Duration = taskDurationsToIndices[task2Duration]
task2Index = indicesWithTask2Duration.pop()
pairedTasks.append([task1Index, task2Index])
return pairedTasks
def getTaskDurationsToIndices(tasks):
taskDurationsToIndices = {}
for idx, taskDuration in enumerate(tasks):
if taskDuration in taskDurationsToIndices:
taskDurationsToIndices[taskDuration].append(idx)
else:
taskDurationsToIndices[taskDuration] = [idx]
return taskDurationsToIndices