Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
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/minimum...
0:00 - Read the problem
2:30 - Drawing Explanation
7:38 - Coding Explanation
leetcode 1443
#neetcode #leetcode #python
Thank you for the daily leetcode problems. For beginners like me , its very helpful. You're a godsend.
Thank you so much for coming back to solve daily problems. These are very helpful! :D
thank you for explaining the daily leetcode problems 👍
you make things super easy! following your channel for a long time now....
Amazing solution and explanation, thank you !
I was able to make it work with 53/54 but stuck for the last test case, and this video literally posted when I decided to seek help. Thank you!
I actually had the same issue as you I'm guessing 😅
@@NeetCodeIO Can you please tell me what was the issue. Or can you tell me the mistake in the code which resulted the last test case to fail
Thanks for the Solution, clearly explained. !
Great explanation! I don't know python. And now I start thinning about learning python because realization is so cool!
Beautiful explanation!!
Thank you so much for your effort it DOES HELP ALOT
I did a double take when I saw the # of subscribers.
Seemed off by a factor of 100.
Now I get it.
welcome back neetcode.
truely amazing!
Awesom explanation
You are a great teacher bro! you changed my coding skills forever! Thank you very very much!😀
Great solution and explanation. Only thing I wonder if using a visited hashset may have made the solution clearer
wouldnt matter it is a tree, all the nodes that will be visited could only be encountered once. ( you cant arrive at same vertex from 2 different paths in a tree because it would be a cycle).
this problem is part of the Trees section in your neetcode list where it should be part of Graphs
agreed
Good explanation
Glad to help!
Will you be posting all new leetcode videos on this channel?
Yes, all future LC videos
Feel like this one should’ve been hard rated
It has to be a noncyclic graph (tree).
Nailed it
please answer this question.
if gennady korotkevich is for codeforce, __________________ is for leetcode?
a, neetcode
b,neetcode
c,neetocode
0:40 why this is not a binary tree ?
Hi !! Thank u so much for this solution !! But can u please can u solve this code by dry run. Actually I got stuck at dfs(4,1). I guess it should return 2 but I come across the continue statement ? :(
This guy must be protected at any cost.
Can anyone explain why was there a need to check if child == parent?
@NeetCodeIO
@@josnamohan9522 while creating adjajency list we are adding the same edge twice e.g. adj[0] has 1 and adj[1] also has 0 otherwise there will be an infinite loop.
@@basecase Thanks for the explanation!
Yeah, Harsh answered it. You might think we don't need to add the edge twice, I did too, but the last test case will fail. Reason is that we need to be able to travel in both directions for this graph, even though it's a tree.
And that test case has a child node, with two parents.
It didn't work using the above code. I think line 17, it doesn't need to add 2 to every childTime. It should be t += childTime. If at least one childTime > 0 then t += 2 (only once )
Omg
C++ Solution for the same code
class Solution {
public:
int minTime(int n, vector& edges, vector& hasApple) {
vector adj(n);
for (auto edge : edges) {
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
return dfs(0, -1, adj, hasApple);
}
int dfs(int curr, int parent, vector& adj, vector& hasApple) {
int time = 0;
for (auto child : adj[curr]) {
if (child != parent) {
int childTime = dfs(child, curr, adj, hasApple);
if (childTime > 0 || hasApple[child]) {
time += (childTime + 2);
}
}
}
return time;
}
};
why not in java
Big Fan of your work bro
Thank you 🙏