Number of Closed Islands - Leetcode 1254 - Python

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

КОМЕНТАРІ • 42

  • @mrmcpherson2722
    @mrmcpherson2722 Рік тому +26

    love the daily leetcode problem vids

  • @ShikaIE
    @ShikaIE Рік тому +17

    Just fyi for others, be careful when visiting neighbors if your dfs return boolean instead of count like here.
    E.g: boolean dfs(grid, r, c) {
    …code here..
    return dfs(grid, r+1, c) &&
    dfs(grid, r-1, c) &&
    ..the rest here..;
    }
    Doing this way will make you stop early and only half the island is visited. When the main for loop reach the unvisited part of the island again, the result will be wrong.

  • @schrodingerskatt222
    @schrodingerskatt222 11 місяців тому +1

    If I'm able to make it to MAANG or any big tech, it would be only because of you Navi. Thankyou so much, I learn a lot from your channel.

  • @patchouli9
    @patchouli9 Рік тому +16

    0:46 Amogus

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

    We can also run the loop from 1 to row_len - 1 and 1 to col_len - 1. Because the land cells in the boundary are never going to be a part of the enclosed islands since minimum one side is boundary.

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

      You could have an island starting at row idx 1 but stretches all the way to boundary.

  • @Andrew-dd2vf
    @Andrew-dd2vf 9 місяців тому

    You can alternatively use the bitwise AND operator (&) to combine the dfs statements, assuming it returns False when OOB and True otherwise.
    Reason: if using "and", once you encounter the first dfs statement and it's false, the remaining dfs statements won't be executed (short-circuit), which will misrepresent the visited grids in future iterations of grid.

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

    I like this person, because he is not a human being 😅😅😅HE REALLY FROM ALIENS 👽👽👽 WORLD 😅😅😅😊😊😊😊(REASON: IF I WATCH SAME PROBLEM IN OTHERS UA-cam CHANNES LIKE 10000 TIMES I CANT ABLE TO UNDERSTAND,SO ALIENS ONLY CREATE WONDERS).SIR PLEASE SUGGEST ME HOW TO BECOME LIKE YOU???
    DEFINITELY HARDWORK MATTERS,OTHER THAN THESE??

  • @MP-ny3ep
    @MP-ny3ep Рік тому

    This solution was so perfectly explained. Thank you.

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

    Awesome explaination as always NEET yor are GOAT of LeetCode

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

    I just don't get why the time complexity is only O(n*m) once you have two nested for loops and additional recursive calls inside of them, wouldn't you have to consider the time complexity of the recursive calls either?

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

    i just watched 5 mins and coded by myself .. and I did it :)

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

    what about the case when there's water inside the land? we're exiting in that case and will run remaining set of land points, resulting in 2 counts for same island?

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

    Great explanation

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

    Great work on the videos and explanation!!

  • @vixguy
    @vixguy Рік тому +2

    Hi NeetCode!
    I tried the same thing but I used true and false instead of 0s and 1s. I have no idea why this turns out to not work for a few testcases.

    • @atishayjain3011
      @atishayjain3011 Рік тому +2

      I just found the reason for this. It's because in the case where you're ANDing all of these traversals, python will stop if it reaches a false condition, whereas you want to go forward with the entire traversal no matter what. The way to do it is to have each statement execute (perhaps set it equal to four variables and then to return the and of those four variables).

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

      @@atishayjain3011 Thanks for the explanation and saving my time !!

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

      Had the same issue. Thanks for mentioning@@atishayjain3011

    • @rixu353
      @rixu353 11 місяців тому

      samer here, I think if you execute False & argment_1, argument_1 won't get executed

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

      @@atishayjain3011And why do we want to visit each cell. If we don’t visit some branch in this iteration, it should remain unvisited and we should cover it when we reach this cell from outer for loop. We would still iterate each cell once only. So, why is this an issue

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

    For the Dfs could you also use booleans and just return Dfs in all directions using and?

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

    Are cpp codes also available? If it s, can you share the same.

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

    importance of (r,c) in visited in base case!!?

  • @ssss-mv6dx
    @ssss-mv6dx Місяць тому

    Why are we returning true for 1 which is not on the boundary?

    • @akhiladevangamath1277
      @akhiladevangamath1277 14 днів тому

      this means, if we have reached water on all 4 direction, that was a closed island, so we are counting it by 1

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

    can someone pls explain why do we return true if we have already visited (r,c) in the base case?

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

    can someone explain what are we doing here: res += dfs(r,c)

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

      6 months late reply, He explains it very well at 08:29

    • @amanrai5285
      @amanrai5285 26 днів тому

      We do that to calculate the number of loops. If dfs return 0 then no closed loops whereas if it returns 1 then we add to the result.

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

    Thank you so much for this video, very helpful! :)
    Just out of curiosity: is there a reason why ROWS and COLS are written in capitals? Is it a convention or just a personal choice?

    • @treyawey3878
      @treyawey3878 Рік тому +2

      It's sort of a convention to write constants in all caps. Makes it easier to identify them. Our company codebase follows this practice at least.

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

    Why can we return true if we have seen it before can anyone explain

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

      Because we're coming back to a piece of land on a grid we've already visited, that means we haven't encountered land on the edge of the grid along that path.

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

  • @SandeepKumar-vk3iq
    @SandeepKumar-vk3iq Рік тому

    Hey bro great content i love your channel however i used this solution and it failed so i am curious why it failed as logic is correct @neetcode

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

    maestro

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

    Great explanation