Instead of returning total, if we included the component in our sum, we could return 0 from the recursion which would solve the way of counting too
@mohammadrouzegar32759 күн бұрын
Lovely solution! I like your approach that avoids using the 'visited' hash set.
@julianelmasry95569 күн бұрын
Self proclaimed and most voted GOAT
@greedyfishbones9 күн бұрын
7:15 the +1 caught so off guard i started wheezing
@strobes26389 күн бұрын
Probably just me having no attention span, but I feel like the contradiction proof explanation wasn't super intuitive even though I solved it before watching, so I wanted to post my thought process even though it's basically the same thing. The way I thought about it was: since the sum of all nodes is divisible by k, whenever we traverse the graph and find a connected set of nodes divisible by k, the sum of the remaining nodes branching off in the graph are also divisible by k. Is there any reason we can't pop it immediately?-> if there are any subtrees branching off not divisible by k that we would "strand". To avoid this, we just work bottom up from the leaf nodes using postorder traversal so we can always pop a component off as soon as the sum is divisible since there's nothing to "strand".
@NeetCodeIO9 күн бұрын
Strand is honestly a really good word for this, thanks for commenting. Really liked the way you explained it.
@strobes26389 күн бұрын
@@NeetCodeIO thanks! I still got a lot from watching your implementation, I wasted a lot of memory on my recursion doing a bunch of stuff like passing a visited node set instead of just keeping track of the parent lmao
@K9Megahertz9 күн бұрын
The real challenge to this problem is finding an applicable use case for it.
@baetz28 күн бұрын
Imagine you have a distributed map-reduce or stream-processing system, and a pool of workers with a tree-like workflow. Some parts of the workflow are calculation-heavy, while others are lighter. Your task is to partition this workflow into equally performant subtrees to put them to different machines. Let's say k is performance unit of a subtree, so if one subtree consumes 2k and other consumes 3k, then you use a cluster of 2 machines for the first subtree and 3 machines for the second.
@K9Megahertz8 күн бұрын
@@baetz2 Why would you use a tree? A flat list of workers would be better no?
@baetz28 күн бұрын
@@K9Megahertz I would use a tree if the workflows were heterogenous and branching. Something where we want to separate concerns, handle errors independently, scale independently. A map-reduce system can be a tree like that with one of the nodes being a pool of mappers and another - a pool of reducers. Disclaimer: I haven't build systems like that irl, it's just my attempt to come up with a use case for this problem.
@caiorenato19 күн бұрын
i wanna be good at leetcode, you are helping me alot, God bless your life my friend.
@henokassalif81759 күн бұрын
My guy just pointed out all the questions I had with the problem examples / description in the first 2 mins of the video
@MP-ny3ep9 күн бұрын
Terrific explanation ! You made a hard problem look easy
@shibambiswas9 күн бұрын
7:15 Bro reminded us about this on the longest night of the year.
@SarweshParsewar9 күн бұрын
yeah true😢
@AadiyKhan9 күн бұрын
6:26 nobody does it better than my goat
@johnniewalkerjohnniewalker24599 күн бұрын
Excellent solution!!!
@sirojiddinSoftwareEngineer8 күн бұрын
Good job
@Everafterbreak_9 күн бұрын
this one demotivated me really bad ngl
@adilansari-hq9ge9 күн бұрын
Good to see you bro!
@kanjurer9 күн бұрын
Neetcode, can you solve the Tandoori Chicken problem next?
@Axel.Blazer9 күн бұрын
what is the number or is this the exact title?
@imranaziz74799 күн бұрын
So basically, every time we run dfs, we are checking to see if we can break off the connection between cur and parent, which will add one more connected component?
@randheerkumargautam64339 күн бұрын
yes
@syuufukuinjp9 күн бұрын
keep it up bro
@ahmedtremo9 күн бұрын
great video as usual :D
@mechano65059 күн бұрын
My goat coming in clutch
@saketh89378 күн бұрын
neetcode to the rescue!
@staywithmeforever8 күн бұрын
13:30 copying 0 😅
@tawfikkoptan57819 күн бұрын
MY GOAT 🔥❤️🔥❤️🔥❤️
@sauravsingh44978 күн бұрын
Its a good day when my goat posts
@prasanna.837 күн бұрын
Can anyone explain why he declines the claim he made earlier regarding that theoretical limit being 3?
@rajsuriyang34278 күн бұрын
The reason we come here is for the math.
@unknown-mf7dd9 күн бұрын
fk this problem
@IamAWESOME39809 күн бұрын
how the hell can people problem-solve to the answer here. this is such bs