Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Painting Wall by andrea.manno1
def checkio(required, operations):
points=None
for n, (start, end) in enumerate(operations):
if points is None:
points=[-start+1, end]
else:
p1, p2=[], []
for point in points:
if abs(point)end:
p2.append(point)
if len(p1)==0 or p1[-1]>0:
p1.append(-start+1)
if len(p2)==0 or p2[0]<0:
p2.insert(0,end)
points=p1+p2
if sum(points)>=required:
return n+1
return -1
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(5, [[1, 5], [11, 15], [2, 14], [21, 25]]) == 1, "1st"
assert checkio(6, [[1, 5], [11, 15], [2, 14], [21, 25]]) == 2, "2nd"
assert checkio(11, [[1, 5], [11, 15], [2, 14], [21, 25]]) == 3, "3rd"
assert checkio(16, [[1, 5], [11, 15], [2, 14], [21, 25]]) == 4, "4th"
assert checkio(21, [[1, 5], [11, 15], [2, 14], [21, 25]]) == -1, "not enough"
assert checkio(1000000011, [[1, 1000000000], [11, 1000000010]]) == -1, "large"
Jan. 30, 2016