This is the question. I'm maintaining two lists, one to store the current possible lengths (list 1 i.e., "len"), another (list 2 i.e., "street") to store the street locations with lights in sorted order (along with starting and endpoints).
My approach is to find the lower bound (just less than the current input) and upper bound for input and add the new lengths of passage without any light and delete the old passage distance between lower and upper bound from the list 1, then insert the input at the appropriate position in the list 2. I'm then printing the max of list 1 (lengths).
Code:
import bisect
x, n = [int(x) for x in input().split()]
lights = list(map(int, input().split()))
# Lists
len, street = [x], [0, x]
i = 0
while(i < n):
r = bisect.bisect_right(street, lights[i]) #upper bound
l = r-1 #lower bound
len.remove(street[r] - street[l])
len.append(lights[i] - street[l])
len.append(street[r] - lights[i])
street.insert(r, lights[i])
print(max(len), end = " ")
i+= 1
I'm getting TLE for large inputs. I feel the "street.insert(r, lights[i])" part of the code is the reason for the error, but, my approach is based on having a sorted list (so that bisect function works to find the lb, ub), so how can I optimise it? Could anyone help me to solve the error?