Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
6 lines proc (commented, no import) solution in Creative category for Flood Area by CDG.Axel
def flood_area(diagram: str):
stack, areas = [], []
for x, e in enumerate(diagram):
stack += [x] * (e == "\\")
if e == "/" and stack and 1 + (st := stack.pop()):
areas = [a for a in areas if a[0] < st] + [(st, x - st + sum(a[1] for a in areas if a[0] > st))]
return [i[1] for i in areas]
# based on my third solution with another improvements, all lines commented below
# line 2 - stack variable contains x-coords where flooded area started
# line 2 - areas variable containt tuples (x-coord where flooded area started, area of it)
# line 4 - add item to stack if landscape goes down, it will be flooded potentially
# line 5 - if landscape goes up take x-coordinate where flooded area started from stack
# line 5 - used succer trick 'expression and True' for initialize st variable in one line with if-state
# line 6 (first expression inside []) - areas left from new one
# line 6 (second expression) - new element contained square of all areas right from new one started
Nov. 26, 2022
Comments: