LeetCode 733. Flood Fill (Algorithm Explained)

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

КОМЕНТАРІ • 46

  • @biswamohandwari6460
    @biswamohandwari6460 4 роки тому +27

    You have the ultimate power to make us understand complex problems very easily that's what makes you powerful. Your the best...

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

    Idk if you read these comments anymore, but I want to say thanks dude. These videos that you've made have been vital to my ability to understand how to approach LeetCode questions.

  • @tushargupta2356
    @tushargupta2356 4 роки тому +19

    your mistake was a good thing to learn from

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

    The most blessed thing is that I am stuck with a question, but Nick has already solved it and made a video.

  • @ahmadalk2540
    @ahmadalk2540 3 роки тому +9

    this line saved my day thanks! I was going with stackOverFlow exception without it.
    if ( image[sr][sc] == newColor ) return image;

  • @vyshnavramesh9305
    @vyshnavramesh9305 4 роки тому +4

    1:21 number of islands, flood fill, rotten oranges.....etc basically bfs n dfs algos

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

    You were right, this is almost identical to number of islands. Once I got this I figured that one out pretty quickly.

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

    Thanks a lot for the clear explanation. Just keep doing this awesome work. It really means a lot to me.

  • @gillupenn
    @gillupenn 18 днів тому

    Using same given function and not creating a separate method (I didn't feel the need)
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
    // find neighbours and add to stack
    int val = image[sr][sc];
    int left = sc-1 >= 0 ? image[sr][sc-1] : -1;
    int right = sc+1 = 0 ? image[sr-1][sc] : -1;
    int down = sr+1

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

    Thank You So for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    The first line is essential; otherwise, the recursion function call will go into an infinite loop.

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

    What is the time and space complexity? I'm guessing time = O(n) and space = O(1). Is that right?

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

    So there are 732 other episodes of this series?

  • @theonly1me
    @theonly1me 3 роки тому +4

    it's kinda funny how Nick is always High :D

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

    Keep doing that awesome work man! Got it instantly, your explanations are very clear.

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

    Thanks so much for sharing this, but doesn't this cause repetition? for eg: void fill method is called for sr=1, sc=1, more than it is required.

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

      image[sr][sc]==newColor, condition has to be added under if condition of void fill?

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

    hello Sir
    i am getting stuck at the below test case
    [[0,0,0],[1,0,0]]
    1
    0
    2

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

    Will this work if the starting point is [0][0] ??

  • @ZEE-fs6hv
    @ZEE-fs6hv 5 років тому +11

    just 850 problems away to complete all leet code problems😀😀

  • @richa6695
    @richa6695 4 роки тому

    we optimize this by not visiting nodes again which are already visited right?

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

      @Richa won't that part get handled in the condition img[sr][sc] !=color ?

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

    just very curious why you need to write another function, is it possible that return it in the same function? I mean use the recursive for the function floodfill

    • @sfitz219
      @sfitz219 4 роки тому

      You could put the old color in a variable at the beginning of the function, and put each of the recursive calls in an if statement, which checks if that index would be out of bounds, and if that pixel is the same color. That was what I tried first, it worked on small images on my computer, it only passed the first 16 test cases on leetcode website before getting a stack overflow on the 17th. When I added a check at the beginning that returns the unmodified image if the oldColor == newColor, it started working. I guess one of the test cases is a huge image where the pixel they try starting the flood fill at is already the color they're filling it with.

  • @harinijeyaraman8789
    @harinijeyaraman8789 4 роки тому

    Your videos are really helpful man ! Thanks a ton

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

    Thanks so much
    Also the last mistake i didn't know about it, thanks man

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

    Can anyone explain why sc >= image[0].length and not sc >= image.length ?

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

      The image will not always be square, so sr is limited to the length of the encompassing array, and sc will be limited to the length the nested array.

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

    Good explanation!

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

    Thanks purge

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

    this is so cool!

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

    Thanks

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

    you are just awesome

  • @DaBestn00b
    @DaBestn00b 4 роки тому

    how do i get 100% faster than everyone else

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

    If the new color is the same as the old color, wouldn't that make an infinite recursion

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

      No, that's caught by line 3. It never even gets to the recursive function. "if (image[sr][sc] == newColor) return image;"

  • @drewanderson5867
    @drewanderson5867 4 роки тому

    Hey the discord link is invalid, can someone send me the link, or comment it

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

    When Asians watching your videos, you know how good you are

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

    GOAT

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

    Decent!

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

    4:21 boi

  • @radwaneberredai6622
    @radwaneberredai6622 5 років тому

    python problém

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

    👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏