use python to implement this algorithm the input should be a nested lsit. e.g.: job = [[0,1],[1,5],[2,3],[2,5],[5,7]] expected result is: [[[0, 1], [1, 5], [5, 7]], [[2, 3], [3, 4]], [[2, 5]]]
def interval_partitioning(job):
#sort the list in the asencding order by the start time,
#which is the 1st element of the inner list
job = sorted(job, key = lambda i: i[0])
n = len(job)
#initilise a list to store the result
ans = []
#add the first item
ans.append([job[0]])
#initialise the depth and last finish
d = 0
#initilise a loop
for i in range(1, n):
for j in range(0,d+1):
if job[i][0] >= ans[j][-1][1]:
ans[j].append(job[i])
break
else:
ans.append([job[i]])
d += 1
return ans
We got this:
[[[0, 1], [1, 5], [5, 7]], [[2, 3], [3, 4]], [[2, 5]], [[2, 5]], [[3, 4]]]