Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Painting Wall by morgothko
from sympy import Interval, Union
def union(data):
intervals = [Interval(begin, end) for (begin, end) in data]
u = Union(*intervals)
return [list(u.args[:2])] if isinstance(u, Interval) else [list(x.args[:2]) for x in u.args]
def checkio(required, operations):
for i in range(len(operations)):
if sum([x[1]-x[0] for x in union(operations[0:i+1])])+1 >= required:
return i+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"
Aug. 23, 2019