Max Area of Island - Leetcode 695 - Python

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ •

  • @NeetCode
    @NeetCode  2 роки тому +3

    🚀 neetcode.io/ - A free resource to prepare for Coding Interviews

  • @travisc9436
    @travisc9436 3 роки тому +70

    Hey NeetCode, just wanted to say thank you for these videos. You do an awesome job at explaining your thought process and how you think about/approach these problems. They've really helped me in my preparation for my interviews. I ended up landing several FAANG offers and a lot of that is due to the content that you provide! Thank you again!

    • @NeetCode
      @NeetCode  3 роки тому +5

      That's awesome Travis, happy I could help!!

    • @Deschuttes
      @Deschuttes 3 роки тому +3

      Nice work. If I dare ask, how'd you get a foot in the door with recruiters for FAANGs?

    • @travisc9436
      @travisc9436 3 роки тому

      @@Deschuttes thanks! Some of them reached out on LinkedIn and others I just applied through their openings.

  • @LeetJourney
    @LeetJourney 10 місяців тому +4

    Solution using BFS:
    class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
    maxAreaSoFar = 0
    visited = set()
    rows = len(grid)
    cols = len(grid[0])
    def bfs(i,j):
    visited.add((i,j))
    q = collections.deque()
    q.append((i,j))
    directions = [[1,0], [-1,0], [0,1], [0,-1]]
    islandArea = 1
    while q:
    r, c = q.popleft()
    for dr,dc in directions:
    row = r + dr
    col = c + dc
    if (row in range(rows) and
    col in range(cols) and
    grid[row][col] == 1 and (row, col) not in visited):
    islandArea += 1
    visited.add((row, col))
    q.append((row, col))
    return islandArea
    for i in range(rows):
    for j in range(cols):
    if grid[i][j] == 1 and (i,j) not in visited:
    maxAreaSoFar = max(maxAreaSoFar, bfs(i,j))
    return maxAreaSoFar

  • @DJSTEVE42
    @DJSTEVE42 Рік тому +1

    Python BFS Code:
    ROW, COL = len(grid), len(grid[0])
    MAX_ISLAND_AREA = 0
    def bfs(r, c):
    q = deque([])
    q.append((r, c))
    grid[r][c] = 0
    area = 0
    while q:
    r, c = q.popleft()
    area+=1
    directions = [[0, 1], [1, 0], [-1, 0], [0, -1]]
    for dr, dc in directions:
    nr, nc = r + dr, c + dc
    if min(nr, nc) >= 0 and nr < ROW and nc < COL and grid[nr][nc] == 1:
    q.append((nr, nc))
    grid[nr][nc] = 0
    return area
    for row in range(ROW):
    for col in range(COL):
    if grid[row][col] == 1:
    MAX_ISLAND_AREA = max(MAX_ISLAND_AREA, bfs(row , col))
    return MAX_ISLAND_AREA

  • @ravishankardas4856
    @ravishankardas4856 2 роки тому +8

    we can remove the usage of set by doing , when we visit a cell having 1 just replace it to 2 or any value other than 1 then in the base case just check if the current cell is not 1 then return 0

    • @ashishchoudhary1664
      @ashishchoudhary1664 8 місяців тому +8

      instead of 2 just set it to 0. We are anyways not visiting 0 and already have a base case defined for it.

  • @milo_andrs
    @milo_andrs 2 роки тому +3

    Thank you, I am studying to become and SDE and this is very helpful in clarifying things for a noob.

  • @stanleychukwu7424
    @stanleychukwu7424 3 роки тому +3

    thanks for uploading, please continue to update your playlist as i usually go back and forth with your playlist

  • @asifchoudhuryca
    @asifchoudhuryca 2 роки тому +13

    Excellent explanation, straightforward solution. Thanks.
    Question: why you have not chosen bfs and the iterative approach?

  • @alexandremarangonicosta
    @alexandremarangonicosta 6 місяців тому

    Besides setting the cell as zero on the grid, you can also do the DFS in a while loop with a list, to save the recursion overhead.

  • @md_pedia1
    @md_pedia1 6 місяців тому +2

    Instead of calling dfs() for every (r,c) , we could instead invoke the function only when grid[r][c]==1.

  • @subjekt5577
    @subjekt5577 2 роки тому +3

    There's a variant from FB where you're allowed to flip one ocean tile to land, and then find the max area of the new island

    • @CodingMachine007
      @CodingMachine007 2 роки тому

      Please let me know where I can find the question or the solution for it

  • @Gameplay-ps9ub
    @Gameplay-ps9ub Рік тому +8

    Hey @NeetCode,
    First you said it's similar to LC200 Number of Islands and there you used BFS, not DFS. I wonder why is that and what did you mean by similar in this case? I used slightly modified version of LC200 BFS in this problem and it worked fine, also ran quicker than 150ms and it seems to be more straight forward solution for me to be honest (this is a matter of opinion though). Cheers!

    • @raghavbhat
      @raghavbhat Рік тому +3

      Dfs and bfs are almost the same, and in this problem can be used interchangeably

    • @EngineeringComplained
      @EngineeringComplained 11 місяців тому +2

      Definitely easier to remember the Number of Islands solution and just modify the bfs to sum the area with each iteration the code above...Particularly in the context of Neetcode where Max Area of Island comes one or two after Number of Islands.

    • @chasegeyer
      @chasegeyer 2 місяці тому

      @@raghavbhat especially since we don't need either of the specific functionalities of bfs or dfs, just need a way to iteratively explore a matrix without making a decision at every point

  • @xinglinli9874
    @xinglinli9874 2 роки тому +1

    The code of NeetCode is very neat and understandable.

  • @mostinho7
    @mostinho7 Рік тому +1

    Done thanks
    Trivial, same as number of islands problem but keep track of largest island

  • @harpercfc_
    @harpercfc_ 2 роки тому +1

    Idk what kind of words could express my gratefulness. You’re doing the God’s job as they said :) thank you again

  • @stepler1503
    @stepler1503 6 місяців тому

    Great video!
    As a small addition - the space complexity could be improved if instead of setting each visited cell into Set data structure you just change the value of the visited cell to 0. This way even visiting it second time it's already counted.
    Also, it could be solved iteratively, using stack.

  • @katherinehill3924
    @katherinehill3924 3 роки тому +2

    Your videos have been so helpful for me. Thank you!

  • @IK-xk7ex
    @IK-xk7ex Рік тому

    Thank you. I could solve the problem by myself less than 15min using the same approach.

  • @ashishchoudhary1664
    @ashishchoudhary1664 8 місяців тому

    A great solution. Instead of keeping visit set, we can just set that position to 0 so we know its already visited.

  • @orellavie6233
    @orellavie6233 2 роки тому +3

    I'd put a check of grid[r][c] == 1 in line 18 as you did in num of islands to just skip a full matrix of 0 all together. OFC same big O, but less runtime in general.

  • @rohanshinde1535
    @rohanshinde1535 2 роки тому

    Thanks @NeetCode I tried without using visit set, awesome explanation.
    class Solution {
    public int maxAreaOfIsland(int[][] grid) {
    int rows = grid.length, cols = grid[0].length;
    int area = 0;
    for(int i=0; i= cols || grid[x][y] == 0) return 0;
    grid[x][y] = 0;
    return 1 + dfs(x+1, y, grid) + dfs(x-1, y, grid) + dfs(x, y+1, grid) + dfs(x, y-1, grid);
    }
    }

  • @luiggymacias5735
    @luiggymacias5735 4 місяці тому

    you can skip the set part by using 0 as visited, and override the grid[i][j] = 0 so it is considered visited

  • @omriovadia9064
    @omriovadia9064 2 роки тому +2

    Hey. Thanks for the solution. How would you replace the "for" loops with a recursive solution? I have a similar problem to solve but no loops allowed.

  • @donaldcodes
    @donaldcodes 2 роки тому

    Beautiful solution. Appreciate everything you do!

  • @andrewchen2349
    @andrewchen2349 2 роки тому

    Very explicit explanation. Thank you so much!

  • @raunaqsingh875
    @raunaqsingh875 2 роки тому

    //C++ Code
    //Algorithm Used: DFS
    //Concept: Just as we count number of island, in this case when converting 1 to 0 we will also count the number of time we did that and after every return fron the DFS function we will assign max with the max value amongst what max already has and what temp has become and finally return the answer.
    void DFS(int i,int j,vector &grid,int &temp){
    if(i==-1 || j==-1 || i==grid.size() || j==grid[0].size() || grid[i][j]==0) return;
    if(grid[i][j]==1){
    temp++;
    grid[i][j]=0;
    DFS(i+1,j,grid,temp);
    DFS(i-1,j,grid,temp);
    DFS(i,j+1,grid,temp);
    DFS(i,j-1,grid,temp);
    }
    }
    int maxAreaOfIsland(vector& grid) {
    int ans=0;
    for(int i=0;i

  • @chaizy1218
    @chaizy1218 8 місяців тому

    best channel I've ever seen ! I appreciate it 💫

  • @nishithshetty4772
    @nishithshetty4772 2 роки тому

    Love your matrix solutions!

  • @Abel-yq1yd
    @Abel-yq1yd 7 місяців тому

    Heres a BFS I came up with(only came up with this cause of following your channel,.. you are goated)
    class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
    if not grid:
    return 0
    rows = len(grid)
    cols = len(grid[0])
    visit = set()
    q = collections.deque()
    def bfs(r, c):
    visit.add((r, c))
    q.append((r, c))
    directions = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    curr_area = 0
    while q:
    row, col = q.popleft()
    curr_area += 1
    for dr, dc in directions:
    r = row + dr
    c = col + dc
    if(r in range(rows) and c in range(cols) and
    (r, c) not in visit and grid[r][c] == 1):
    q.append((r, c))
    visit.add((r, c))
    return curr_area
    area = 0
    for r in range(rows):
    for c in range(cols):
    if (r, c) not in visit and grid[r][c] == 1:
    val = bfs(r, c)
    area = max(area, val)
    return area

  • @ramvenkatachalam8153
    @ramvenkatachalam8153 7 місяців тому

    Bro , u are a god .

  • @dhavalmavani2464
    @dhavalmavani2464 Рік тому

    Instead of using hashmap for visited land we can just change the value of visited land from '1' to '2'
    So that way code will use less space and also by doing this you won't have to check if the land is visited or not

    • @freyappari
      @freyappari Рік тому

      he mentions that at the end bro

  • @kawinm3594
    @kawinm3594 2 роки тому +1

    Note for myself: Ask the interviewer if we can modify the array instead of extra visit set.

  • @cn7abc
    @cn7abc 2 роки тому +6

    Why did we use BFS for the number of islands problem but DFS for this problem??

    • @anyalauria1364
      @anyalauria1364 2 роки тому +1

      same question

    • @bchen1403
      @bchen1403 2 роки тому +3

      BFS can also be applied here. There is really no advantage between one another in terms of complexity.

    • @adewumisunkanmi5593
      @adewumisunkanmi5593 2 роки тому +4

      Using DFS looks more concise in my opinion

    • @infernape716
      @infernape716 6 місяців тому

      variety

  • @harsh21012
    @harsh21012 2 роки тому

    I don't think there's a need to use another data structure to mark the visited nodes, just make them equal to any other arbitrary value other than 0,1 say 2 and that shall do it.

  • @aditheorigin4926
    @aditheorigin4926 2 роки тому

    Hi NeetCode, in a real interview I was explaining this solution. I was questioned the time complexity and said O(mxn). But he mentioned that in case of hash collision the complexity is N. My question to you is shall we not consider is as O(1) or O(n)?
    TIA

    • @adventurer2395
      @adventurer2395 2 роки тому

      Average case is O(1), but when a collision happens, we could argue that it's O(N) because it might be iterating through a linked list at that index to find the value (also known as chaining).

  • @hwang1607
    @hwang1607 10 місяців тому

    could you add a condition to check if grid[r][[c] == 1 in the bottom nested loop to optimize it?

  • @kirillzlobin7135
    @kirillzlobin7135 5 місяців тому

    Amazing video

  • @dionmokhtari3602
    @dionmokhtari3602 Рік тому

    Quick Question: How does this solution differentiate to count Spaces that have "1" vs "0" . In the Number of island problems you had this check? @NeetCode

  • @shivaprasad8919
    @shivaprasad8919 2 роки тому

    I think you missed `+` between the all dfs calls.
    return 1 + dfs(r+1, c) + dfs(r-1, c) + dfs(r, c+1) + dfs(r, c-1)

  • @roguenoir
    @roguenoir 2 роки тому +1

    If you don't mind modifying the input, you can simply mark the visited cells with 0 instead of using a hash set. Leetcode still accepted my submission with that strategy.

  • @paragsarkar8258
    @paragsarkar8258 3 роки тому +2

    Suppose i have an unsorted list [1,8,86,34,77,18,98,16] how can i find the two closest integers with constant time .
    I'm not using any sort function..only hash..can anyone help me ?

    • @rahulprasad2318
      @rahulprasad2318 3 роки тому +7

      Not possible in O(1)
      I think the best approach would be to use heap.

  • @shirishvogga6046
    @shirishvogga6046 2 роки тому +3

    Using BFS is much easier though like it has no recursion lol
    and has the same time complexity too

  • @Philgob
    @Philgob 4 місяці тому

    shouldn't we add a
    if (r,c) in visited: continue
    in the last double for loop? he said that in the explanation but didnt add it to the code

  • @CS_n00b
    @CS_n00b Рік тому

    How do we solve this if we are also allowed to change the order of the columns?

  • @lordbeerus4546
    @lordbeerus4546 2 роки тому

    If we can convert only one 0 of our choice to 1, then how can we find the max area of land?
    Brute soln will take O((m×n)×(m×n))
    How can we optimize it to O(m×n) ?

  • @DARON121
    @DARON121 Рік тому

    Thanks for the video, but i don't get why this one is in the Graph category on your website🥴

  • @yqiu9707
    @yqiu9707 3 роки тому

    why (r, c) is in visit set, we have to return 0?

    • @porassingh4904
      @porassingh4904 3 роки тому +1

      if that element in the grid is already visited you don't want to run dfs on it again as it is already computed and will increase the time complexity. And once calculated it won't add up to the area right? So in that case we return 0.
      Hope it helps!

    • @bchen1403
      @bchen1403 2 роки тому

      Poras was right, and I tried removing the visit set portion and got RuntimeError for maximum recursion depth exceeded. So by having a visit set, it's improving our time complexity.

  • @coffeebytez
    @coffeebytez 2 роки тому

    🔥🔥🔥

  • @levileuthold2459
    @levileuthold2459 Місяць тому

    Why is this problem considered a "Graph" problem when we don't actually use a graph to solve it?

  • @MohitRaj-1712
    @MohitRaj-1712 3 роки тому

    I thought dfs will give me MaximumRecursionDepth Error in Python.

    • @hantinglu8050
      @hantinglu8050 3 роки тому +2

      set the visited island to 0 and only visit the cell with value 1

  • @mohamedmosad7702
    @mohamedmosad7702 2 роки тому

    😍😍

  • @moviesbest9090
    @moviesbest9090 Рік тому

    I have solved this question using the same code as your no. of islands video (using bsf), It works !
    class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
    if not grid:
    return 0
    rows = len(grid)
    cols = len(grid[0])
    maxArea = 0
    visited = set()
    # bfs function
    def bfs(r, c):
    nonlocal maxArea
    area = 1
    queue = collections.deque()
    visited.add((r,c))
    queue.append((r,c))
    while queue:
    for _ in range(len(queue)):
    row, col = queue.popleft()
    directions = [[1,0], [-1,0], [0, -1], [0 , 1]]
    for dr, dc in directions:
    if ((dr + row) in range(rows) and
    (dc + col) in range(cols) and
    grid[dr + row][dc + col] == 1 and
    (dr + row, dc + col) not in visited):
    visited.add((dr + row, dc + col))
    queue.append((dr + row, dc + col))
    area += 1
    maxArea = max(maxArea , area)

    #for each and every position , call bfs
    for i in range(rows):
    for j in range(cols):
    if (i,j) not in visited and grid[i][j] == 1:
    bfs(i , j)
    return maxArea

  • @meera8362
    @meera8362 2 роки тому +1

    Recursive way: Code might be seems to be big but it is just a DRY code.
    class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
    visited_islands = set()
    res = 0
    before = 0
    def recursive(r, c):
    # check the top
    if r > 0 and grid[r - 1][c]==1 and ((r-1, c)) not in visited_islands:
    visited_islands.add((r - 1, c))
    recursive(r - 1, c)
    # check the bottom
    if r < len(grid) - 1 and grid[r + 1][c]==1 and ((r+1, c)) not in visited_islands:
    visited_islands.add((r + 1, c))
    recursive(r + 1, c)
    # check the left
    if c > 0 and grid[r][c - 1]==1 and ((r, c-1)) not in visited_islands:
    visited_islands.add((r, c - 1))
    recursive(r, c - 1)
    # check the right
    if c < len(grid[0]) - 1 and grid[r][c + 1]==1 and ((r, c+1)) not in visited_islands:
    visited_islands.add((r, c + 1))
    recursive(r, c + 1)

    for r in range(len(grid)):
    for c in range(len(grid[0])):
    print(r, c)
    if grid[r][c] == 1 and (r, c) not in visited_islands:
    visited_islands.add((r, c))
    recursive(r, c)
    res = max(res, len(visited_islands) - before)
    before = len(visited_islands)
    #res = max(res, recursive(r, c, 0))
    return res