Skip to content

Instantly share code, notes, and snippets.

@kmarenov
Created March 17, 2021 12:41
Show Gist options
  • Select an option

  • Save kmarenov/8d8605e1c63cb08c2dc79995ab87ca56 to your computer and use it in GitHub Desktop.

Select an option

Save kmarenov/8d8605e1c63cb08c2dc79995ab87ca56 to your computer and use it in GitHub Desktop.
islands = [
[0, 1, 1, 1, 1],
[0, 1, 1, 0, 1],
[0, 0, 0, 1, 0],
[1, 1, 1, 0, 0],
];
def islands_num(matr):
m = len(matr)
if m == 0:
return 0
n = len(matr[0])
if n == 0:
return 0
num = 0
for i in range(m):
for j in range(n):
if matr[i][j] == 1:
if num == 0:
num += 1
continue
if i - 1 > 0 and matr[i - 1][j] == 1:
continue
elif j - 1 > 0 and matr[i][j - 1] == 1:
continue
elif i + 1 < m and matr[i + 1][j] == 1:
continue
elif j + 1 < n and matr[i][j + 1] == 1:
continue
else:
num += 1
return num
print(islands_num(islands))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment