-- Oct 16 In-Class Exercise Thread
Pseudo code
input_list = [[5, 9], [8, 12], [15, 20], [1, 10], [9, 11], [3,8]]
sorted_list = sort(input_list) based on the first element of each interval.
for i in range (0, len(sorted_list))
if sorted_list[i] = X then
continue
for j in range (1, len(sorted_list))
if sorted_list[i][0] > sorted_list[j][0] and sorted_list[i][1] > sorted_list[j][1] then
Replace sorted_list[i] with X
GC_list = Delete X from the sorted_list
Runtime
For sorting - O(nlog(n))
For comparing the intervals - O(n^2)
Final runtime - O(n^2)
(
Edited: 2019-10-21)
Pseudo code
input_list = [[5, 9], [8, 12], [15, 20], [1, 10], [9, 11], [3,8]]
sorted_list = sort(input_list) based on the first element of each interval.
for i in range (0, len(sorted_list))
if sorted_list[i] = X then
continue
for j in range (1, len(sorted_list))
if sorted_list[i][0] > sorted_list[j][0] and sorted_list[i][1] > sorted_list[j][1] then
Replace sorted_list[i] with X
GC_list = Delete X from the sorted_list
Runtime
For sorting - O(nlog(n))
For comparing the intervals - O(n^2)
Final runtime - O(n^2)