class Solution: def findMaxFish(self, grid: List[List[int]]) -> int: row = len(grid) col = len(grid[0]) visited = [[False]*col for _ in range(row)] ans = 0 # first start with the land for i in range(row): for j in range(col): if grid[i][j]>0 and not visited[i][j]: ans = max(ans,self._dfs(grid,visited,i,j)) return ans def _dfs(self, grid:List[List[int]],visited:List[List[bool]], i:int, j:int) -> int: if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or visited[i][j] or grid[i][j] == 0: return 0 visited[i][j] = True current_part = grid[i][j] + self._dfs(grid,visited,i+1,j) + self._dfs(grid,visited,i-1,j) + self._dfs(grid,visited,i,j+1) + self._dfs(grid,visited,i,j-1) return current_part
like target for this video is 170. Please do like if you have understood the explanation as well as the code😊
Love the way you explain Shahswat. ♥♥
nice explain sir
thnx a lot bhaiya, keep teaching like that.
Thanks for the solution. Btw why dont u add ur face cam like older videos. it was cool.
♥♥♥♥♥♥
Thank you so much 😃
thankyou for the explanation ❤
@@PiyushSharma-we8yd 🙏🙏
class Solution:
def findMaxFish(self, grid: List[List[int]]) -> int:
row = len(grid)
col = len(grid[0])
visited = [[False]*col for _ in range(row)]
ans = 0
# first start with the land
for i in range(row):
for j in range(col):
if grid[i][j]>0 and not visited[i][j]:
ans = max(ans,self._dfs(grid,visited,i,j))
return ans
def _dfs(self, grid:List[List[int]],visited:List[List[bool]], i:int, j:int) -> int:
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or visited[i][j] or grid[i][j] == 0:
return 0
visited[i][j] = True
current_part = grid[i][j] + self._dfs(grid,visited,i+1,j) + self._dfs(grid,visited,i-1,j) + self._dfs(grid,visited,i,j+1) + self._dfs(grid,visited,i,j-1)
return current_part
08:13 definately error because of if row or col are out of grid then how can you access then
this problem is exactly the same as 1219. Path with Maximum Gold just without backtracking
@@manishchauhan4630 ya kind of
first
error due to not-shortcircuit of grid[r][c] will come