Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Painting Wall by Amachua
# This function update the tuple d by taking into account the new data e.
# The output is the amount of value in the N-space which aren't already taken into account in d.
def update(d, e):
# minmin and maxmax are the lowest and highest value for the new added set in d.
# minmax and maxmin are the values needed to compute the number of new painted walls.
# intersect is also used for the computation of the painted walls, it's for the old set contains in the new one.
minmin, minmax, maxmax, maxmin, intersect = e[0], e[0], e[1], e[1], 0
for i in range(len(d)):
a = d.pop(0)
# update the limit by taking into account the value of the data a.
if a[0] <= e[0] <= a[1]:
minmin = a[0] if a[0] < minmin else minmin
minmax = a[1] if a[1] < maxmin else maxmin
if a[0] <= e[1] <= a[1]:
maxmax = a[1] if a[1] > maxmax else maxmax
maxmin = a[0] if a[0] > minmax else minmax
# if the data a is in the set e update the intersect value.
# Check if the upper or lower value was updated.
# If it's not add it in d.
if a[0] > e[0] and a[1] < e[1]:
intersect+=a[1]-a[0]
else:
d.append(a)
if maxmin - minmax != 0:
d.append((minmin, maxmax))
return maxmin - minmax - intersect
def checkio(num, data):
a = []
for i, d in enumerate(data, 1):
num -= update(a, d)
print(a, num)
if num <= 1: return i
return -1
Feb. 28, 2014