I am trying to use an algorithm for the problem "Max area of island" in a 3D problem, so it would be more like max volume of island. I was using total volumes of 200x200x200 voxels as input, but I am having trouble because it does not work when there are very big 'islands' in the volume I input ('core dumped' in the Ubunut terminal). Here is the code with the modifications I did to apply it to my 3D problem:
class Solution3D:
def dfs3D(self, grid, r, c, l):
grid[r][c][l] = 0
num = 1
lst = [(r-1, c, l), (r+1, c, l), (r, c-1, l), (r, c+1, l), (r, c, l-1), (r, c, l+1)]
for row, col, leh in lst:
if row >= 0 and col >= 0 and leh >= 0\
and row < len(grid) and col < len(grid[0]) and leh < len(grid[0][0])\
and grid[row][col][leh] == 1:
num += self.dfs3D(grid, row, col, leh)
return num
def maxAreaOfIsland3D(self, grid):
area_islands = 0
for r in range(len(grid)):
for c in range(len(grid[0])):
for l in range(len(grid[0][0])):
if grid[r][c][l] == 1:
area_islands = max(area_islands, self.dfs3D(grid, r, c, l))
return area_islands
Is this implementation too inefficient? How could I make it less memory hungry so that I can use it with big islands?
Thank you very much!