Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Painting Wall by brownie57
import bisect
def checkio(required, operations):
a = {list[0] for list in operations}
b = {list[1]+1 for list in operations}
array = sorted(list(a|b))
c = 0
painted = set()
while c < len(operations):
n = 0
l = bisect.bisect_left(array, operations[c][0])
r = bisect.bisect_left(array, operations[c][1])
c += 1
for x in range(l,r):
painted.add((array[x], array[x+1]))
for y in painted:
n += y[1]-y[0]
if n >= required:
return c
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"
July 17, 2018