-
-
Notifications
You must be signed in to change notification settings - Fork 47k
[Matrix] Max area of island problem solved DFS algorithm #6918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 25 commits into
TheAlgorithms:master
from
kondekarshubham123:matrix/max_area_of_island
Oct 18, 2022
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
27210cf
Maximum area of island program added
kondekarshubham123 6cbfc10
Update matrix/max_area_of_island.py
kondekarshubham123 8e34544
Update matrix/max_area_of_island.py
kondekarshubham123 08decd3
Update matrix/max_area_of_island.py
kondekarshubham123 41e37a5
Review's comment resolved
kondekarshubham123 b36b08d
max area of island
kondekarshubham123 ce4e66c
PR Comments resolved
kondekarshubham123 26a1a3a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6fb6295
Test case fail fix
kondekarshubham123 c06bd9c
Grammer correction
kondekarshubham123 071d060
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9855474
flake8 issue resolved
kondekarshubham123 e385611
some variable name fix
kondekarshubham123 34f92b5
Merge branch 'TheAlgorithms:master' into matrix/max_area_of_island
kondekarshubham123 8eb98e8
Update matrix/max_area_of_island.py
kondekarshubham123 9b5338b
Update matrix/max_area_of_island.py
kondekarshubham123 3af0a52
PR, comments resolved
kondekarshubham123 78aabac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 094a4ce
Update matrix/max_area_of_island.py
kondekarshubham123 1f6464d
Update matrix/max_area_of_island.py
kondekarshubham123 5898399
PR, comments resolved
kondekarshubham123 7142723
Merge branch 'matrix/max_area_of_island' of https://github.com/kondek…
kondekarshubham123 cc0c96a
Update max_area_of_island.py
cclauss 7bbdc55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 323047f
Typo
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
Given an two dimensional binary matrix grid. An island is a group of 1's (representing | ||
land) connected 4-directionally (horizontal or vertical.) You may assume all four edges | ||
of the grid are surrounded by water. The area of an island is the number of cells with | ||
a value 1 in the island. Return the maximum area of an island in a grid. If there is no | ||
island, return 0. | ||
""" | ||
|
||
matrix = [ | ||
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], | ||
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], | ||
[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], | ||
] | ||
|
||
|
||
def is_safe(row: int, col: int, rows: int, cols: int) -> bool: | ||
""" | ||
Checking whether coordinate (row, col) is valid or not. | ||
|
||
>>> is_safe(0, 0, 5, 5) | ||
True | ||
>>> is_safe(-1,-1, 5, 5) | ||
False | ||
""" | ||
return 0 <= row < rows and 0 <= col < cols | ||
|
||
|
||
def depth_first_search(row: int, col: int, seen: set, mat: list[list[int]]) -> int: | ||
""" | ||
Returns the current area of the island | ||
|
||
>>> depth_first_search(0, 0, set(), matrix) | ||
0 | ||
""" | ||
rows = len(mat) | ||
cols = len(mat[0]) | ||
if is_safe(row, col, rows, cols) and (row, col) not in seen and mat[row][col] == 1: | ||
seen.add((row, col)) | ||
return ( | ||
1 | ||
+ depth_first_search(row + 1, col, seen, mat) | ||
+ depth_first_search(row - 1, col, seen, mat) | ||
+ depth_first_search(row, col + 1, seen, mat) | ||
+ depth_first_search(row, col - 1, seen, mat) | ||
) | ||
else: | ||
return 0 | ||
|
||
|
||
def find_max_area(mat: list[list[int]]) -> int: | ||
""" | ||
Finds the area of all islands and returns the maximum area. | ||
|
||
>>> find_max_area(matrix) | ||
6 | ||
""" | ||
seen: set = set() | ||
kondekarshubham123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
max_area = 0 | ||
for row, line in enumerate(mat): | ||
for col, item in enumerate(line): | ||
if item == 1 and (row, col) not in seen: | ||
# Maximizing the area | ||
max_area = max(max_area, depth_first_search(row, col, seen, mat)) | ||
return max_area | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
print(find_max_area(matrix)) # Output -> 6 | ||
|
||
""" | ||
Explanation: | ||
We are allowed to move in four directions (horizontal or vertical) so the possible | ||
in a matrix if we are at x and y position the possible moving are | ||
|
||
Directions are [(x, y+1), (x, y-1), (x+1, y), (x-1, y)] but we need to take care of | ||
boundary cases as well which are x and y can not be smaller than 0 and greater than | ||
the number of rows and columns respectively. | ||
|
||
Visualization | ||
mat = [ | ||
[0,0,A,0,0,0,0,B,0,0,0,0,0], | ||
[0,0,0,0,0,0,0,B,B,B,0,0,0], | ||
[0,C,C,0,D,0,0,0,0,0,0,0,0], | ||
[0,C,0,0,D,D,0,0,E,0,E,0,0], | ||
[0,C,0,0,D,D,0,0,E,E,E,0,0], | ||
[0,0,0,0,0,0,0,0,0,0,E,0,0], | ||
[0,0,0,0,0,0,0,F,F,F,0,0,0], | ||
[0,0,0,0,0,0,0,F,F,0,0,0,0] | ||
] | ||
|
||
For visualization, I have defined the connected island with letters | ||
by observation, we can see that | ||
A island is of area 1 | ||
B island is of area 4 | ||
C island is of area 4 | ||
D island is of area 5 | ||
E island is of area 6 and | ||
F island is of area 5 | ||
|
||
it has 6 unique islands of mentioned areas | ||
and the maximum of all of them is 6 so we return 6. | ||
""" | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.