Created
March 17, 2021 12:41
-
-
Save kmarenov/8d8605e1c63cb08c2dc79995ab87ca56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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