Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Calculate Islands by Oleg_Domokeev
from typing import List
def checkio(land_map: List[List[int]]) -> List[int]:
H, L, result = len(land_map), len(land_map[0]), []
while True:
try:
n_row = [any(row) for row in land_map].index(True)
except ValueError:
break
stack, size = {(n_row, land_map[n_row].index(1))}, 0
while len(stack):
size += len(stack)
for a, b in stack:
land_map[a][b] = 0
stack = {(a, b) for a in range(H) for b in range (L) for n, m in stack if abs(a-n) < 2 and abs(b-m) < 2 and land_map[a][b]}
result.append(size)
return sorted(result)
Dec. 23, 2018
Comments: