Sir, in the problem statement posted by Leetcode in today's daily challenge demands to calculate the minimum number of moves. It took me around 30-40 minutes to understand the significance of the word "minimum". I was thinking about multisource BFS, but could not progress further.
thank you mazhar sir...i first attempted this question 7-8 months back and after not approaching and not understanding through different video tutorials I've decided to left this question and now I totally understood this question thank you...
Finally I found a channel that ...explains concepts exactly the way I understand. Its really helpful.Thankyou for the initiative .Keep up the good work.
Thanks Bhai 12:30 yaha tak dekha almost aagaya tha par ye example fail hora tha Input: root = [0,3,0] Output: 3 then "story to code" timestamp wala part dekha got an idea of if child is 0 then we have to send -1. Once again Thanks bhai ❤️❤️
i write things just like you did, but was unsure how to add those up in moves to ans, i messed with code and i don't know why i wrote abs(left)+abs(right), but it got accepted. maybe my knowleged and thought process is increasing by doing potd and watching ur solutions
Very Nice explanation.Just one query ..where do you find so much variety of problems for a single topic, is there any list u following/or any forums?or random pick
Hi, I am not getting one thing here. In JAVA code (from your github links), I am able to see you have used one moves array, and adding value at it 0th index and returning 0th index. But for C# it seems moves is an integer variable. That makes sense! What I wanted to know is why in JAVA we used an array instead of moves as integer variable?(In that case I am getting error as well). Would be great, if you please explain!
In Java, primitive data types like int are passed by value, meaning when you pass an int to a method, a copy of the value is made and passed to the method. Changes made to the parameter inside the method do not affect the original value outside the method. To achieve similar behavior to passing by reference in C++, you can use an array or an object. In the provided Java code: public static int distributeCandy(Node root) { if (root == null || (root.left == null && root.right == null)) { return 0; } int[] moves = {0}; solve(root, moves); return moves[0]; } Here, moves is an array of size 1. Arrays are objects in Java, and when you pass an array to a method, you are passing a reference to the array object. Inside the distributeCandy method, moves[0] is modified by the solve method, effectively mimicking the behavior of passing moves by reference. private static int solve(Node root, int[] moves) { if (root == null) { return 0; } int l = solve(root.left, moves); int r = solve(root.right, moves); int totalExtraCandies = (l + r + root.data) - 1; moves[0] += Math.abs(l) + Math.abs(r); return totalExtraCandies; } The solve method takes an array moves as a parameter. Inside the solve method, the value of moves[0] is updated to reflect the total moves made. Since arrays are objects in Java, changes made to the array inside the solve method will be reflected outside the method, similar to passing by reference in C++.
bro ek baat batatyo ki recursion question mein hum question puchte hai ki jaise aap ne question mein pucha ki left and right se kitni extra candies ye kaise pata chalta hai ki hum recursion se kya question puche ki wo hamara sub problem solve karde
can anybody please explain kinda a proof how this equation { moves += abs(l) + abs(r) } works not able to visualise that coins/candy movement how adding that extra on left and right subtree gives us total moves
Actually you can ALWAYS write a separate function of your own to call in recursion. What I always do is that whenever you have to pass an extra parameter which is not present in the main function (provided by leetcode), then I write my own function. If I don’t have to pass any extra parameter, then the given function can itself be used to call in recursion.
class Solution { int ans=0; public int distributeCandies (Node root){ dfs(root); return ans; } public int dfs(Node root){ if(root==null) return 0; int l=dfs(root.left); int r=dfs(root.right); ans+=Math.abs(l)+Math.abs(r); return root.data+l+r-1; } } 🎉❤
Hi bhaiya aaj 1 month baad contest diya. Solved 2 question but took 1.5hrs, earlier used to do in 30-40 min. Looks like exams had a big toll on me :( Kya karu , is it normal ??
Output will be 4. Left most leaf(0) will return -1 to it's parent Then node(4) k lie totalExtraCandies = (-1 + 0 + 4) - 1 = 2 Now it will calculate moves = abs(l) + abs(r) = 1; return totalExtraCandies = 2 Not node(0) i.e. root will get l = 2 from its left child as calculated above and similarly it will get r = -1 from it's right child. moves += abs(l) + abs(r) moves = 1 + (abs(l) + abs(r)) moves = 1 + (2 + 1) = 4
For every Candidate who gave up on DSA you adds a new Story in their life.
❤️❤️
For me too. I am into DSA because of this legend.
True
true i'm the one
true. this guys is a legend
🔥🔥 Let’s make this guy famous.
He deserves it
ekdum se inhone waqt badal diya, jasbat badal diye, coding ke halat badal diye... GOAT for a reason
The explanation of this problem is truly outstanding, leaving no room for confusion.
thank you Sir 😀
Story wale Bhaiyyaa🤩
thank you. u are superb!!!!!
😇🙏❤️
Hats off to your unique style of teaching. Every point cleared.
Similar Leetcode Problem (POTD Today of 18th May, 2024) - leetcode.com/problems/distribute-coins-in-binary-tree
Sir, in the problem statement posted by Leetcode in today's daily challenge demands to calculate the minimum number of moves. It took me around 30-40 minutes to understand the significance of the word "minimum". I was thinking about multisource BFS, but could not progress further.
thankyou sir came here cause of the potd, but will be staying cause of your amazing narration.
Thanks sir for the best explaination please bring GFG POTD solution regularly just like LC POTD .
10 mins into your video I got the idea how to write the story .. thanks for making DSA easy as a cake ... couldn't have done without you
thank you mazhar sir...i first attempted this question 7-8 months back and after not approaching and not understanding through different video tutorials I've decided to left this question and now I totally understood this question thank you...
Finally I found a channel that ...explains concepts exactly the way I understand. Its really helpful.Thankyou for the initiative .Keep up the good work.
You are most welcome 🙏🙏
bhai hats off to yaar. kya hi amazing tarika hai aapke explain karne ka. just mind blowing
awesome story leading to awesome algo
Best explanation so far! You never disappoint us!
Glad you think so! 😇🙏❤️
Thanks for teaching DSA in this story wise way.
i think i might be one of your earliest students... since 2.5k subscribers...
❤️❤️🙏🙏
Me too bro
Amazingggg explanation specially the negative move part
best solution for this question so far
Very nice explanation
Did the question on my own
Thanks a lot😇
you explanation is amazing. you have given me hope.
Amazing explanation, crystal clear!
thanks for this AMAZING story solution.
Thanks Bhai 12:30 yaha tak dekha almost aagaya tha par ye example fail hora tha Input: root = [0,3,0] Output: 3 then "story to code" timestamp wala part dekha got an idea of if child is 0 then we have to send -1. Once again Thanks bhai ❤️❤️
i write things just like you did, but was unsure how to add those up in moves to ans, i messed with code and i don't know why i wrote abs(left)+abs(right), but it got accepted. maybe my knowleged and thought process is increasing by doing potd and watching ur solutions
❤️❤️
loved the approach
waiting for 50k bhaiya
wow bhaiya!!! impressive!!!
Was eagerly waiting for this video thanks a lot bhaiya ❣️
brilliant explanation!
Thankyou bhaiya!
Thanks a lot bhaiya ❤❤
bhaiya ek request h jaise aap phle hrr question ke phle motivation quote dette the 2 min ka vo fir se continue kr do pls
Noted on this ❤️🙏
thanks sir great explanation ☝
Hi sir, can you please make a video on questions to get started for competitive programming on Codeforces. Or guide for the same.
Great explaination ✨ Sir please upload solutions for biweekly contest if possible..
Great video 👌🏻
Please make a video on todays leetcode contest question 4.
bhai kya bawal samjhate ho , maja aa jata h.
Nice Explanation!!
Very Nice explanation.Just one query ..where do you find so much variety of problems for a single topic, is there any list u following/or any forums?or random pick
Thank you sir ...
Hi, I am not getting one thing here. In JAVA code (from your github links), I am able to see you have used one moves array, and adding value at it 0th index and returning 0th index. But for C# it seems moves is an integer variable. That makes sense! What I wanted to know is why in JAVA we used an array instead of moves as integer variable?(In that case I am getting error as well). Would be great, if you please explain!
In Java, primitive data types like int are passed by value, meaning when you pass an int to a method, a copy of the value is made and passed to the method. Changes made to the parameter inside the method do not affect the original value outside the method.
To achieve similar behavior to passing by reference in C++, you can use an array or an object. In the provided Java code:
public static int distributeCandy(Node root) {
if (root == null || (root.left == null && root.right == null)) {
return 0;
}
int[] moves = {0};
solve(root, moves);
return moves[0];
}
Here, moves is an array of size 1. Arrays are objects in Java, and when you pass an array to a method, you are passing a reference to the array object. Inside the distributeCandy method, moves[0] is modified by the solve method, effectively mimicking the behavior of passing moves by reference.
private static int solve(Node root, int[] moves) {
if (root == null) {
return 0;
}
int l = solve(root.left, moves);
int r = solve(root.right, moves);
int totalExtraCandies = (l + r + root.data) - 1;
moves[0] += Math.abs(l) + Math.abs(r);
return totalExtraCandies;
}
The solve method takes an array moves as a parameter. Inside the solve method, the value of moves[0] is updated to reflect the total moves made. Since arrays are objects in Java, changes made to the array inside the solve method will be reflected outside the method, similar to passing by reference in C++.
@@codestorywithMIK Thanks a lot for this much brief!🙂
Now it makes sense!
You are unique.
please make more and more videos on graphs
Guruji. Itna acha koi kaise parha sakta hai bhai.
Water the plants on gfg ,ka code jo apne karaye hai similar questions Leetcode 1326 par work nahi kar raha hai...why?
They are not entirety same. One difference you can see that in GFG, the element can also be 0
But in leetcode all number will be > 0
@@codestorywithMIK code me kis point par change karne hoga.
Kay app code me change kare ke GitHub par push kar sakte hai (for Leetcode_1326)?
Thank You Sir :)
great explaination
bro ek baat batatyo ki recursion question mein hum question puchte hai ki jaise aap ne question mein pucha ki left and right se kitni extra candies ye kaise pata chalta hai ki hum recursion se kya question puche ki wo hamara sub problem solve karde
❤❤❤❤❤❤waited
today's gfg problem vertex cover ki bhi video upload krdo bhaiya
One day when I'll make it to google, I will not forget to mention you.
This means a lot. I am sure you will reach your goal. ❤️
can anybody please explain kinda a proof how this equation { moves += abs(l) + abs(r) } works not able to visualise that coins/candy movement how adding that extra on left and right subtree gives us total moves
Can you plz, make a vdo on this question from leetcode - 1993. Operations on Tree, It's a request
Hats off 🙏🏻
Thanku bhaiya ❤
bhya please solve this question
leetcode 1366 :
Rank Team by Votes..
I'm facing lot of problem in optimization of this problem.
please have a look
Will soon check ❤️
i love you bhaiya 😭😭😭😭
Love you all 😇❤️🙏
Keep working hard.
We all will rock
Mik sir enable topmate...
how did you arrive at the conclusion that moves should be l+r
Thank you 🥹
when to create a new function and when to just use the given one only ?
Actually you can ALWAYS write a separate function of your own to call in recursion.
What I always do is that whenever you have to pass an extra parameter which is not present in the main function (provided by leetcode), then I write my own function.
If I don’t have to pass any extra parameter, then the given function can itself be used to call in recursion.
bhai aapko sirf voice se hi pehchante hai apna face bhi reveal krdo kabhi😅😅😂
class Solution {
int ans=0;
public int distributeCandies (Node root){
dfs(root);
return ans;
}
public int dfs(Node root){
if(root==null) return 0;
int l=dfs(root.left);
int r=dfs(root.right);
ans+=Math.abs(l)+Math.abs(r);
return root.data+l+r-1;
}
}
🎉❤
Hi bhaiya aaj 1 month baad contest diya. Solved 2 question but took 1.5hrs, earlier used to do in 30-40 min. Looks like exams had a big toll on me :( Kya karu , is it normal ??
Don’t worry. It happens.
Keep giving contests no matter what.
Bhaiya we want regular gfg potd also ❤
Yes @codestorywithMIK
sir gfg pr aapka java wala solution error dera h please check...
Updated the code now. It works fine now ❤️
👌🏻👌🏻
Sir Today GFG Problem??????
Unfortunately I couldn’t get time today.
Will definitely try to upload tomorrow 🙏🙏
Question kya puchna hai recursion mei, yhi issue hain 😢
sir agar coins bich wale node per hua toh
matlab
0
4 0
0
phir kya hoga?
Output will be 4.
Left most leaf(0) will return -1 to it's parent
Then node(4) k lie totalExtraCandies = (-1 + 0 + 4) - 1 = 2
Now it will calculate moves = abs(l) + abs(r) = 1;
return totalExtraCandies = 2
Not node(0) i.e. root will get l = 2 from its left child as calculated above and similarly it will get r = -1 from it's right child.
moves += abs(l) + abs(r)
moves = 1 + (abs(l) + abs(r))
moves = 1 + (2 + 1) = 4
bhai tu goat hain yaar sab samaj aagaya fr
You are O+
Means a lot 🙏
🙇♂🙇♂🙇♂
❤
suggest some more problems so that I can confident in making stories before coding the tree problem
Sure.
Try Maximum Width Of Binary Tree
Binary Tree Maximum Path Sum
❤
Bhai thora ad kam kri yar..30 min. 8-10 ad .. concentration kharab ho rha h