Number of Good Paths - Leetcode 2421 - Python

Поделиться
HTML-код
  • Опубликовано: 18 сен 2024

Комментарии • 39

  • @NeetCodeIO
    @NeetCodeIO  Год назад +13

    This problem was a huge pain in the ass to explain but I hope this was helpful. If you're unfamiliar with UnionFind you can checkout this video: ruclips.net/video/FXWRE67PLL0/видео.html

    • @salim444
      @salim444 Год назад

      thank you NC. I feel like you're sick, Get well soon ❤️

    • @ankurbose7672
      @ankurbose7672 4 месяца назад

      can you please explain in the naive solution how to handle the duplicate case? For example: 1->2 == 2->1. It will become difficult if we maintain a set of existing good paths and reverse the current path to check if it exists or not.

  • @scresat
    @scresat Год назад +46

    This problem makes me want to leave it all behind and live the rest of my life in the Himalayas away from humanity.

  • @Raymond-Wu
    @Raymond-Wu Год назад +10

    You've described these as crackhead problems in the past. It'd be funny if you had a playlist for those types of problems as this one definitely belongs in there!

  • @AR_7333
    @AR_7333 Год назад +8

    Hey,
    I think the self.rank need to initialized with list of 1's.
    self.rank = [1] * n
    Because otherwise:
    self.rank[bRoot] += self.rank[aRoot]
    will be just adding 0's. Thus have no efficiency benefit.

    • @NeetCodeIO
      @NeetCodeIO  Год назад +1

      Yep, that's correct. My mistake.

  • @sypher4735
    @sypher4735 Год назад +1

    I just watch you for logical part and I get it. Thanks for the videos.

  • @mucle6
    @mucle6 Год назад

    You can do a sortof implicit union find using the graph. First make the graph, then when you go through it, if you see a value less than you , remove that node, and update its neighbors to be your neighbors. Then stop when you find a node larger than you

  • @hackytech7494
    @hackytech7494 Год назад +9

    Thank you so much ♥, I was waiting for you to upload this. Please keep uploading Daily streak problems 🙏🙏🙏🙏🙏🙏🙏🙏🙏

    • @MP-ny3ep
      @MP-ny3ep Год назад

      Did the code work for you?

  • @CostaKazistov
    @CostaKazistov Год назад +1

    Highly underrated channel.
    This was a top-notch code walkthrough of LC Hard problem.

  • @odysy5179
    @odysy5179 Год назад +2

    This video is clutch

  • @narendrayadav71
    @narendrayadav71 Год назад

    Thanks a ton !! It was really easy to understand !!

  • @yang5843
    @yang5843 Год назад +2

    i'm a little confused at 11:20, how is 0->2->3 a good path? the values of the path are 1->2->1, which breaks the second condition of a good path

    • @NeetCodeIO
      @NeetCodeIO  Год назад

      I was referring to 1 -> 0 -> 2 -> 4 at that moment.

  • @bittu007ize
    @bittu007ize Год назад +1

    Awesome JOB!!!!

  • @kuklama0706
    @kuklama0706 12 часов назад

    This is basically how Quake BSP works right?

  • @user-bsuyegwiwu
    @user-bsuyegwiwu Год назад +1

    I don't understand the last part of the code (# For each disjoint set, how many val’s does it contain?), how does it calculate the number of val's in each disjoin set? Can someone please explain to me? Thansk!

    • @noneyourbussiness5879
      @noneyourbussiness5879 3 месяца назад

      In a disjoint set, all will have same parent value. ( all nodes in dishoint set will lead to a root after UF).
      so for eavh val in disjoint set we increment count[parent]

  • @sankararao7563
    @sankararao7563 14 дней назад

    why do we even need to find neighbors, If the node has one occurrence. In this case for the node 2 value 2, we don't even need to run union find for the node 2

  • @nuamaaniqbal6373
    @nuamaaniqbal6373 Год назад

    Why do we need to traverse the vals in sorted order ? Why is that necessary ? What would not work if we didnt?

  • @alexd627
    @alexd627 Год назад

    OMG you have a new chaneLL???? nicee

  • @yang5843
    @yang5843 Год назад

    For anybody attempting this solution in Java, you need to change the rank to 1 instead of 0 otherwise you'll get TLE.

  • @MP-ny3ep
    @MP-ny3ep Год назад +1

    Thank you! I was waiting for you to upload this haha

  • @rashzh5502
    @rashzh5502 Год назад

    Thanks! Please keep making videos on Daily challenges!

  • @podilichaitanyaakhilkumar4911
    @podilichaitanyaakhilkumar4911 Год назад

    This is a very difficult problem, but you made it easy. Upload more hard problems with all possible approaches. For medium level questions leetcode discuss section is enough to understand, but it is quite difficult to understand hard problems from discuss section. Hence we need video solution for hard problems. Please make videos on hard problems. Thank you.

  • @iliadmitriev01
    @iliadmitriev01 Год назад

    what's the reason to initialize ranks of vetices at UnionFind class with 0's
    I think it's a mistake, should be initialized with 1's

    • @NeetCodeIO
      @NeetCodeIO  Год назад +1

      1 would work as well. Keep in mind the ranks are only used to compare with each other, so as long as they all have the same starting value it works out.

    • @yang5843
      @yang5843 Год назад

      ​@@NeetCodeIOYes it works out because the code handles ambiguous situations where the ranks are equal, but the final rank of every node will be 0 if you used your existing code. I encountered a TLE with this solution in java because of it

    • @NeetCodeIO
      @NeetCodeIO  Год назад

      @@yang5843 Ah, yeah you're right.

  • @王梦如-f5f
    @王梦如-f5f Год назад

    What will happen if I do the unionFind in the opposite order? 3->2->1

  • @honestreviewlarki3410
    @honestreviewlarki3410 Год назад

    Can anyone tell me why my code isn't working?
    `
    class Solution:
    def numberOfGoodPaths(self, vals: List[int], edges: List[List[int]]) -> int:
    l = len(vals)
    if len(set(vals)) == l: return l
    g = collections.defaultdict(set)
    for a, b in edges:
    g[a].add(b)
    g[b].add(a)
    print(g)
    self.ans = 0
    vis = set()
    def dfs(node):
    self.ans += 1
    vis.add(node)
    path = []
    for c in g[node]:
    if c in vis: continue
    if vals[node] >= vals[c]:
    path.append(node)
    path.append(c)
    print(path)
    dfs(c)

    return self.ans

    for i in range(0,l):
    dfs(i)
    return self.ans
    `

  • @satyajeetdas6577
    @satyajeetdas6577 Год назад +1

    I came up with dfs solution as soon as i saw the question, but the main problem is how i will know to come up with union find algo for this problem.. i mean what is the intuition behind to determine we will use union find. bcoz i have practised a quite a no of graph problem where the intuition to use union find is quite recognizable but here it is out of box!!!!! please let me know the intuition behind it!!!!!! @NeetCodeIO