Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
20-liner: what else? for-else! solution in Clear category for Flood Area by przemyslaw.daniel
def merge_overlapping(data):
result = {}
for (a, b) in data[::-1]:
for (c, d) in result:
if c < a < b < d:
result[(c, d)] += b-a
break
else:
result[(a, b)] = b-a
return result
def flood_area(data):
groups, stack = [], []
for index, char in enumerate(data):
if char == '\\':
stack += [index]
if stack and char == '/':
groups += [(stack.pop(), index)]
result = merge_overlapping(groups)
return [result[x] for x in sorted(result)]
Feb. 20, 2019