Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Painting Wall by mortonfox
def add_zone(zones, zone):
newzones = []
for z in zones:
if z[1] < zone[0] - 1 or z[0] > zone[1] + 1:
newzones.append(z)
else:
zone = [min(z[0], zone[0]), max(z[1], zone[1])]
newzones.append(zone)
return newzones
def count_sections(zones):
return sum(z[1] - z[0] + 1 for z in zones)
def checkio(required, operations):
zones = []
for i, op in enumerate(operations, 1):
zones = add_zone(zones, op)
if count_sections(zones) >= required:
return i
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"
Nov. 10, 2017
Comments: