LeetCode Max Area of Island Solution Explained - Java

  Рет қаралды 13,678

Nick White

Nick White

Күн бұрын

Пікірлер: 19
@_ParijatChatterjee
@_ParijatChatterjee 3 жыл бұрын
instead of that extra seen boolean, before the return statement in the area function just write grid[r][c] = 0.
@Bryan-gu5fg
@Bryan-gu5fg 2 жыл бұрын
can u explain why we can use grid[r][c] instead of seen boolean?
@gabrieltimothy2255
@gabrieltimothy2255 2 жыл бұрын
@@Bryan-gu5fg Firstly by setting already visited grid[r][c] to 0 we can similarly guarantee that our function will not mistakenly count it again as an island on the next iterations (since islands are identified as 1). Secondly, we might prefer to do this instead as it is more efficient in terms of space complexity, i.e. we don't need to allocate space in memory for another 2 dimensional array for 'seen'.
@juanvibeats3880
@juanvibeats3880 2 жыл бұрын
Here's an optimized version in JavaScript, instead of having the seen global variable, we can simply set the 1s equal to 0 once we've viewed them. Got the idea from Nick's video on 'Number of Islands' leetcode problem: /** * @param {number[][]} grid * @return {number} */ var maxAreaOfIsland = function(grid) { let maxArea = 0; for(let i = 0; i < grid.length; i++) { for(let j = 0; j < grid[0].length;j++) { if(grid[i][j] === 1) { let areaOfIsland = dfs(grid,i,j); if(areaOfIsland > maxArea) { maxArea = areaOfIsland; } } } } return maxArea; }; const dfs = (matrix, i, j, area) => { if(i < 0 || j < 0 || i >= matrix.length || j >= matrix[0].length || matrix[i][j] === 0) { return 0; } if(!area) area = 1; area += 1; matrix[i][j] = 0; let up = dfs(matrix, i - 1, j, area); let down = dfs(matrix, i + 1, j, area); let left = dfs(matrix, i, j - 1, area); let right = dfs(matrix, i, j + 1, area); return 1 + up + down + right + left; }
@alankuo2316
@alankuo2316 5 жыл бұрын
great explanation!!!
@vikrant236
@vikrant236 3 жыл бұрын
Hey Nick, did you study any other questions too for your tech interviews. If you can share complete list you solved before interview that would be great!
@zahash1045
@zahash1045 4 жыл бұрын
OMG!! HE'S SO GOD DAMN COOL!!!
@joecamroberon9322
@joecamroberon9322 5 жыл бұрын
Can you explain why you are returning 1 + the recursive call?
@NickWhite
@NickWhite 5 жыл бұрын
hey yeah because the cell is valid. If the cell is not valid than the function returns 0 immediately, but if it is valid we add 1 to the total area
@NickWhite
@NickWhite 5 жыл бұрын
if we didn't add 1 at some point than the area would never grow and always be zero
@yogendramaarisetty
@yogendramaarisetty 2 жыл бұрын
above recursion is something like this example: f(n) = 1 + f(n-1) { n>=0, f(0) = 0 } lets say n = 4 f(4) = 1 + f(3) f(4) = 1 + (1 + f(2)) f(4) = 1+ (1 + (1 + f(1))) f(4) = 1+(1+(1+(1+f(0)))) f(4) = 4
@lifehacks9450
@lifehacks9450 4 жыл бұрын
could u pls upload word ladder 2 problem
@vishaldhanani3942
@vishaldhanani3942 4 жыл бұрын
It is not working in the test case of [[0],[1]]. Please explain it. Thanks
@yogendramaarisetty
@yogendramaarisetty 2 жыл бұрын
I will work!
@ZEEEDgenx
@ZEEEDgenx 3 жыл бұрын
Just to check if I'm understanding, we should return 0 on a seen island because, if we don't and we keep recursively visiting that island our area for the max area would go to infinity because it keeps checking its neighbor and its neighbor keeps checking it.
@yesh5943
@yesh5943 3 жыл бұрын
I think we return 0 because the area of a 0 cell is not part of an island and does not contribute to its area.
@pankajas6448
@pankajas6448 2 жыл бұрын
time limit exceeded bro :(
@aryandeshpande1241
@aryandeshpande1241 3 жыл бұрын
Ayoo
@msantagiulianab
@msantagiulianab 4 жыл бұрын
"Time Limit Exceeded", time-complexity is too high.
Amazon Coding Interview Question - Rotate Image
17:26
Nick White
Рет қаралды 245 М.
Max Area of Island - Leetcode 695 - Graphs (Python)
11:15
Greg Hogg
Рет қаралды 3,6 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 8 МЛН
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 8 МЛН
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1,1 МЛН
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 745 М.
Max Area of Island - Leetcode 695 - Python
11:14
NeetCode
Рет қаралды 64 М.
Coding Was Hard Until I Learned THESE 5 Things!
8:02
Nick White
Рет қаралды 33 М.
Are Women Smarter than Men?
18:12
Memeable Data
Рет қаралды 33 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 159 М.
LeetCode 735. Asteroid Collision (Solution Explained)
14:24
Nick White
Рет қаралды 21 М.
LeetCode Spiral Matrix Solution Explained - Java
11:07
Nick White
Рет қаралды 49 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 185 М.