Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Itertools FTW solution in Creative category for Painting Wall by nicuveo
from functools import reduce
from itertools import dropwhile
from itertools import accumulate
_i = lambda rs, r: reduce(_a, sorted(rs + [r]), [])
_s = lambda rs: sum(1 + b - a for a, b in rs)
_g = lambda d: map(_s, accumulate([[]] + d, _i))
def _a(rs, r):
if not rs: return [r]
s, l = rs[:-1], rs[-1]
return s + [[l[0], max(l[1], r[1])]] if l[1] >= r[0] - 1 else rs + [r]
def checkio(n, d):
try: return next(dropwhile(lambda x: x[1] < n, enumerate(_g(d))))[0]
except: 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"
assert checkio(20,[[1,2],[20,30],[25,28],[5,10],[4,21],[1,6]]) == 5, "bigger one"
May 30, 2014