-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity_selection.py
More file actions
28 lines (25 loc) · 946 Bytes
/
Copy pathactivity_selection.py
File metadata and controls
28 lines (25 loc) · 946 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
25
26
27
28
# https://www.geeksforgeeks.org/activity-selection-problem-greedy-algo-1/
class Solution:
def activitySelection(self, start_times, end_times) -> list:
graph = []
for start, end in zip(start_times, end_times):
graph.append([start, end, end - start])
# sort the graph by finishing times
graph = sorted(graph, key=lambda x: x[1])
result = [graph[0]]
current_end_time = graph[0][1]
index = 1
print(graph)
while index < len(graph):
if graph[index][1] <= current_end_time:
index = index + 1
else:
result.append(graph[index])
current_end_time = graph[index][1]
index = index + 1
return result
if __name__ == '__main__':
start_time = [1, 3, 0, 5, 8, 5]
end_time = [2, 4, 6, 7, 9, 9]
sol = Solution()
print(sol.activitySelection(start_time, end_time))