Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Painting Wall solution in Clear category for Painting Wall by 1-more
# migrated from python 2.7
def checkio(required, operations):
def is_intersect(x,y):
return y[0]<= x[0] <=y[1] or x[0]<= y[0] <=x[1]
painted= set()
for i,x in enumerate(operations):
discard= set()
for y in painted:
if is_intersect(x,y):
x= [min(x[0],y[0]), max(x[1],y[1])]
discard.add(tuple(y))
painted-= discard
painted.add(tuple(x))
if sum([x[1]-x[0]+1 for x in painted])>=required: return i+1
return -1
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(15,[[1,2],[20,30],[25,28],[5,10],[4,21],[1,6]]) == 4, "2/2"
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"
Oct. 18, 2015