Construct Quad Tree - Leetcode 427 - Python

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    đŸ„· Discord: / discord
    🐩 Twitter: / neetcode1
    🐼 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: ‱ Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: ‱ House Robber - Leetco...
    Problem Link: leetcode.com/problems/constru...
    0:00 - Read the problem
    0:50 - Drawing Explanation
    7:00 - Coding Explanation
    leetcode 427
    #neetcode #leetcode #python

Komentáƙe • 37

  • @yang5843
    @yang5843 Pƙed rokem +22

    NeetCode is killing it at explaining these problems!

  • @fierce2321
    @fierce2321 Pƙed rokem +4

    Great explanation. One thing I wanted to note - we dont have to use "isSame" on all elements, we can use recursive call output for that. Here is my solution with complexity O(n^2):
    class Solution:
    def construct(self, grid: List[List[int]]) -> 'Node':
    n = len(grid)
    def helper(rowSt, colSt, gridSize):
    if gridSize == 1:
    return Node(grid[rowSt][colSt], 1, None, None, None, None)
    childSize = gridSize//2
    topLeft = helper(rowSt, colSt, childSize )
    topRight = helper(rowSt, colSt+ childSize ,childSize )
    bottomLeft = helper(rowSt+childSize, colSt ,childSize )
    bottomRight = helper(rowSt+childSize, colSt+childSize ,childSize )
    isLeaf = topLeft.isLeaf and topRight.isLeaf and bottomLeft.isLeaf and bottomRight.isLeaf and topLeft.val == topRight.val and topLeft.val == bottomLeft.val and topLeft.val == bottomRight.val
    val = topLeft.val
    isLeafInt = 0
    if isLeaf:
    isLeafInt = 1
    topLeft,topRight,bottomLeft, bottomRight = None, None, None, None
    root = Node(val, 1 if isLeaf else 0, topLeft, topRight,bottomLeft, bottomRight )
    # print(rowSt, colSt, gridSize, ":", root.isLeaf, root.val, topLeft.val, topRight.val,bottomLeft.val, bottomRight.val)
    return root
    root = helper(0,0,n)
    return root

  • @ferb7o2
    @ferb7o2 Pƙed rokem +1

    WOW, JUST WOW; had the idea in mind but had no clue how to convert it to code, as always THANK YOU!

  • @DarkMetroid777
    @DarkMetroid777 Pƙed rokem

    Super easy explanation, thanks!

  • @ajins777
    @ajins777 Pƙed rokem +2

    This explanation was a lot more concise to the point and very articulate and easy to follow. I didn't know if you took my feedback, but if you did then, YOU the MAN!! ✹

  • @adithyasaish2122
    @adithyasaish2122 Pƙed rokem

    Amazing Explanation!

  • @ax5344
    @ax5344 Pƙed 4 měsĂ­ci +3

    The problem description looks likes an essay. I was totally blown away not even wanting to know what it tries to say.
    Thanks for doing videos on this sort of problems!

  • @plontulublalulu
    @plontulublalulu Pƙed rokem +1

    I didn’t understand this problem until watching this video. Thanks neetcode

  • @mohithadiyal6083
    @mohithadiyal6083 Pƙed rokem

    Helpful as usual 👍

  • @krateskim4169
    @krateskim4169 Pƙed rokem

    Awesome explanation

  • @viktoreidrien7110
    @viktoreidrien7110 Pƙed rokem

    neat code indeed man, thank you

  • @ifychim3189
    @ifychim3189 Pƙed měsĂ­cem

    Great explanation man thank you !!!

  • @vietnguyenquoc4948
    @vietnguyenquoc4948 Pƙed 7 měsĂ­ci

    Sick explanation and implementation. I hope someday I can be as good as you in implementing the solution...

  • @matusklement5029
    @matusklement5029 Pƙed rokem +1

    You can actually speed this up to O(n^2) if you'll just check if all the children are leafs. If so, then we'll make a shortcut with their value if all children have that value same.

  • @sathwikmadhula9527
    @sathwikmadhula9527 Pƙed rokem

    For break statement inside 2 for loops,. Does it break both the for loops or only one ?

  • @roywastaken
    @roywastaken Pƙed rokem +2

    here every day dude!

  • @m.kamalali
    @m.kamalali Pƙed rokem +9

    Who else smashed like button before getting into video

  • @MP-ny3ep
    @MP-ny3ep Pƙed rokem +2

    Thank you , the problem statement was a bit hard to understand , your explanation was too good.

  • @firezdog
    @firezdog Pƙed 5 měsĂ­ci

    My question for this one would be why the pruning is actually beneficial. If you don't prune but just use logic to figure out when to dump a recursive sub-result and when to use it, you will recurse until you visit ever square in the grid. But you excuse yourself the n^2 operation of scanning the sub-grid to see if it is the same. In the worst case you have to do all those scans and then you also have to recurse down to the individual cells. But in other cases the overhead for the recursive calls is worse. Still, wouldn't the theoretical big-O be better?

  • @gottuparthiharichandana3381

    Good! But what is the use of this quad tree data structure. Like do we use it in real time implementations?

  • @cesarroa5484
    @cesarroa5484 Pƙed 2 měsĂ­ci

    Do we have to check if the root can be divided?

  • @adityavats3990
    @adityavats3990 Pƙed rokem

    I think we can save the O(n^2) of checking whether quad is full of 0s or 1s.This can be precomputed just once in O(n^2)

  • @pro3757
    @pro3757 Pƙed rokem +1

    We can avoid checking for same values by using the output of the recursive call. Just return a flag if all children have same values. That way each cell is visited only once, taking time complexity down to n^2.

    • @yaswanthkosuru
      @yaswanthkosuru Pƙed rokem +2

      practice and understanding others coding ==success

    • @firezdog
      @firezdog Pƙed 5 měsĂ­ci

      but in practice it doesn't seem this is more efficient (doesn't get you to the left in Leetcode's tests, even with successive tries to account for their inaccuracy) b/c the overhead for the recursive calls is terrible. Figuring out the exact flag can be a bit tricky too -- at least was for me. But it seems much more conceptually elegant.

  • @carantesferreira
    @carantesferreira Pƙed 11 měsĂ­ci

    It would be really useful if put some disclaimers in the screen when you have this kind of typO ("n" instead of "i" or "j") , I was really frustrated trying to understand why you were adding "n" to the grid coordinates. Thanks for making such great videos with wonderful explanations

  • @cinimodmil
    @cinimodmil Pƙed rokem +1

    Thanks for this neet! Really neat! To everyone else: does anyone know why we wouldn't go out of bounds when we're adding i and j to r and c respectively i.e., if grid[r][c] != grid[r + i][c + j]? I printed it out n it looks okay doesnt seem to go out of bounds.

    • @akashnegi913
      @akashnegi913 Pƙed rokem +1

      r , c - even though we are adding n at each recursive call, we are also diving it by 2 which will always be with 0, n range
      i, j - Bounded from 0 to n - 1

    • @cinimodmil
      @cinimodmil Pƙed rokem

      @Akash Negi Ah! Thank you akash!

  • @RV-qf1iz
    @RV-qf1iz Pƙed rokem

    how to build logic like you can you make a video on that

  • @GFC1337
    @GFC1337 Pƙed rokem +1

    Just in time a day before my uber onsite

    • @dojoPojo
      @dojoPojo Pƙed rokem

      How was it ?
      Did you joined ? What type of ques were asked ?

    • @GFC1337
      @GFC1337 Pƙed rokem

      @@dojoPojo Onsite got canceled because the position got filled, then hiring froze for a bit. My recruiter then got laid off. Recently, however, I contacted a different recruiter and I was able to secure an onsite which should take place in a couple of weeks. Hopefully that doesn't get cancelled as well. Fortunately, my OA + tech screen results are valid for 6 months, which means they will be valid for about 2 more lol.

    • @dojoPojo
      @dojoPojo Pƙed rokem +1

      @@GFC1337 All the best !

  • @yang5843
    @yang5843 Pƙed rokem +4

    class Solution {
    public Node construct(int[][] grid) {
    return dfs(grid,grid.length,0,0);

    }
    Node dfs(int[][] grid, int N, int n, int m) {
    boolean same = true;
    for (int i=0;i

  • @alexl2512
    @alexl2512 Pƙed 2 měsĂ­ci

    This is not the optimal solution. Recursively break down the block until there is one cell and compose the parent node is the way to go. It's a O(lgn) solution.