Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python

  Рет қаралды 19,958

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 44
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Thank you for the daily leetcode problems. For beginners like me , its very helpful. You're a godsend.
@FatCowFat
@FatCowFat Жыл бұрын
thank you for explaining the daily leetcode problems 👍
@arpanbanerjee6224
@arpanbanerjee6224 Жыл бұрын
you make things super easy! following your channel for a long time now....
@shinewbiez
@shinewbiez Жыл бұрын
Thank you so much for coming back to solve daily problems. These are very helpful! :D
@laumatthew71
@laumatthew71 Жыл бұрын
Amazing solution and explanation, thank you !
@picnicbros
@picnicbros Жыл бұрын
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!
@NeetCodeIO
@NeetCodeIO Жыл бұрын
I actually had the same issue as you I'm guessing 😅
@arpanbanerjee6224
@arpanbanerjee6224 Жыл бұрын
@@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
@asmisrivastava3934
@asmisrivastava3934 2 ай бұрын
@@arpanbanerjee6224 pass adjacency matrix by refrence and it will work
@phaneendhrachinta7295
@phaneendhrachinta7295 Жыл бұрын
Thanks for the Solution, clearly explained. !
@readonlylogin
@readonlylogin Жыл бұрын
Great explanation! I don't know python. And now I start thinning about learning python because realization is so cool!
@CostaKazistov
@CostaKazistov Жыл бұрын
I did a double take when I saw the # of subscribers. Seemed off by a factor of 100. Now I get it.
@krishnavamsi9326
@krishnavamsi9326 Жыл бұрын
You are a great teacher bro! you changed my coding skills forever! Thank you very very much!😀
@kapilchoudhary2922
@kapilchoudhary2922 Жыл бұрын
Beautiful explanation!!
@rsKayiira
@rsKayiira Жыл бұрын
Great solution and explanation. Only thing I wonder if using a visited hashset may have made the solution clearer
@nihalbhandary162
@nihalbhandary162 6 ай бұрын
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).
@TheSmashten
@TheSmashten Ай бұрын
Can you do the problem "Making A Large Island"?
@xcampus
@xcampus Жыл бұрын
welcome back neetcode.
@Kilvny
@Kilvny Жыл бұрын
Thank you so much for your effort it DOES HELP ALOT
@mohamedsalama2743
@mohamedsalama2743 7 ай бұрын
this problem is part of the Trees section in your neetcode list where it should be part of Graphs
@timmyfrank3
@timmyfrank3 4 ай бұрын
agreed
@xcampus
@xcampus Жыл бұрын
please answer this question. if gennady korotkevich is for codeforce, __________________ is for leetcode? a, neetcode b,neetcode c,neetocode
@kenmatsuru3470
@kenmatsuru3470 Жыл бұрын
truely amazing!
@krateskim4169
@krateskim4169 Жыл бұрын
Awesom explanation
@shensean1784
@shensean1784 Жыл бұрын
It has to be a noncyclic graph (tree).
@nsandeep7574
@nsandeep7574 Жыл бұрын
Good explanation
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Glad to help!
@imaadf5124
@imaadf5124 Жыл бұрын
Will you be posting all new leetcode videos on this channel?
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Yes, all future LC videos
@cj-nr5ni
@cj-nr5ni Жыл бұрын
Nailed it
@noahgsolomon
@noahgsolomon Жыл бұрын
Feel like this one should’ve been hard rated
@parikshitjuneja
@parikshitjuneja Жыл бұрын
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; } };
@ojaswiawasthi3645
@ojaswiawasthi3645 Жыл бұрын
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 ? :(
@josnamohan9522
@josnamohan9522 Жыл бұрын
Can anyone explain why was there a need to check if child == parent?
@josnamohan9522
@josnamohan9522 Жыл бұрын
@NeetCodeIO
@basecase
@basecase Жыл бұрын
@@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.
@josnamohan9522
@josnamohan9522 Жыл бұрын
@@basecase Thanks for the explanation!
@NeetCodeIO
@NeetCodeIO Жыл бұрын
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.
@aakashgoswami2356
@aakashgoswami2356 Жыл бұрын
This guy must be protected at any cost.
@englishtechnique4813
@englishtechnique4813 Жыл бұрын
why not in java
@baomao139
@baomao139 9 ай бұрын
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 )
@dera_ng
@dera_ng Жыл бұрын
Omg
@thomas_shelbyyt1349
@thomas_shelbyyt1349 Жыл бұрын
Big Fan of your work bro
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Thank you 🙏
Find Duplicate Subtrees - Leetcode 652 - Python
14:33
NeetCodeIO
Рет қаралды 20 М.
Winning Google Kickstart Round A 2020 + Facecam
17:10
William Lin (tmwilliamlin168)
Рет қаралды 9 МЛН
She wanted to set me up #shorts by Tsuriki Show
0:56
Tsuriki Show
Рет қаралды 8 МЛН
Andro, ELMAN, TONI, MONA - Зари (Official Music Video)
2:50
RAAVA MUSIC
Рет қаралды 2 МЛН
Hilarious FAKE TONGUE Prank by WEDNESDAY😏🖤
0:39
La La Life Shorts
Рет қаралды 44 МЛН
Dijkstra's Algorithm - Computerphile
10:43
Computerphile
Рет қаралды 1,3 МЛН
Most Common Concepts for Coding Interviews
6:08
NeetCode
Рет қаралды 335 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 147 М.
Rotating the Box - Leetcode 1861 - Python
15:14
NeetCodeIO
Рет қаралды 4,9 М.
Design Browser History - Leetcode 1472 - Python
13:30
NeetCodeIO
Рет қаралды 20 М.
Unique Binary Search Trees II - Leetcode 95 - Python
12:51
NeetCodeIO
Рет қаралды 19 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 576 М.
Time Needed to Inform All Employees - Leetcode 1376 - Python
10:42
She wanted to set me up #shorts by Tsuriki Show
0:56
Tsuriki Show
Рет қаралды 8 МЛН