Maximum Product of Splitted Binary Tree -(Google, Meta, Amazon(Phone)) : Explanation ➕ Live Coding

  Рет қаралды 4,040

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 58
@prateekrath5098
@prateekrath5098 Жыл бұрын
Brilliant explanation as always sir! please keep making the videos
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Prateek. I will for sure ❤️❤️❤️
@manishv.8167
@manishv.8167 Жыл бұрын
Definately He is Awesome
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Manish ❤️❤️❤️
@codestorywithMIK
@codestorywithMIK Жыл бұрын
The time complexity of this one is a little straight forward. If you notice, we are visiting each node of the Tree just Once. So Time complexity will be : O(n) Where n = number of nodes in the tree. You can say we have called the function twice , - one for calculating totalSum - And another for calculating maximum product. So to be precise , Time complexity will be O(n +n) = O(2*n) which is effectively O(n) only. (Ignoring the constant 2). I hope that helped. Thanks to all of you for watching 😇
@nagmakhan672
@nagmakhan672 Жыл бұрын
The intuition and using same function to Update maxP was 🔥 🔥 🔥
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Man this explanation is legit 🔥. You are seriously Hindi Version of NeetCode. Soon, you will be reaching millions of subs
@piyushacharya7696
@piyushacharya7696 Жыл бұрын
Reach++ arey bhai kya ans hai🔥🔥🔥.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Piyush ❤️❤️❤️
@oqant0424
@oqant0424 Жыл бұрын
lesson learnt today: never judge a youtuber by his no of subscribers....... Man !!!!!!!!!!! u are a legit 🔥🙌
@codestorywithMIK
@codestorywithMIK Жыл бұрын
This comment made my day. I will sleep in peace Thank you so much ❤️❤️❤️
@oqant0424
@oqant0424 Жыл бұрын
@@codestorywithMIK i will thank myself today that i found your channel.......seriously
@oqant0424
@oqant0424 Жыл бұрын
@@codestorywithMIK please never stop making daily leetcode challenge solutions as your explanation will help a lotttttttttttt in logic building............literally no one on the whole youtube teaches how to build logic.....except u and the great Aditya Verma sir!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot ❤️❤️❤️
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure thing. I will be there always ❤️
@wearevacationuncoverers
@wearevacationuncoverers Жыл бұрын
You are phenomenal. This channel is a treasure. please keep making the videos
@debugging_life
@debugging_life Жыл бұрын
The intuition and explanation is amazing. It would be really helpful if you can explain how to calculate time complexities of such questions.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Arpana. Actually the time complexity of this one is a little straight forward. If you notice, we are visiting each node of the Tree just Once. So Time complexity will be : O(n) Where n = number of nodes in the tree. You can say we have called the function twice , one for calculating totalSum And another for calculating maximum product. So to be precise , Time complexity will be O(n +n) = O(2*n) which is effectively O(n) only ignoring the constant 2. I hope that helped. Thank you again for watching 😇
@debugging_life
@debugging_life Жыл бұрын
@@codestorywithMIK yes, It helped. Thanks !
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Thumbnail me jo likha hai wo hi dekhkar maine like kar Diya
@sounishnath513
@sounishnath513 Жыл бұрын
Baapre. You just gave the whole concept in just a minute. 😮😮. Seriously these videos are awesome ❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so so much Sounish ❤️❤️❤️
@pankajjangra7
@pankajjangra7 Жыл бұрын
Bdhiya padhate ho bhai👌👌
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much Pankaj ❤️❤️❤️
@oqant0424
@oqant0424 Жыл бұрын
one earnest feedback :) plz discuss time and space complexities at the end!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure thing. Noted
@yatri6329
@yatri6329 Жыл бұрын
Hard h yaar , sum wala approach se easy ho gya, otherwise smjh nhi aa rha tha
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
True yaar. sum wala approach changed everything
@anuppal3243
@anuppal3243 Жыл бұрын
Very nice and detailed explanation sir.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Anup ❤️❤️❤️
@gui-codes
@gui-codes 7 ай бұрын
Awesome explanation
@danianiazi8229
@danianiazi8229 Жыл бұрын
Love the explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot 😇
@madhuraj4you
@madhuraj4you Жыл бұрын
hatsoff
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so so much Madhuraj ❤️❤️❤️
@ramankr0022
@ramankr0022 8 ай бұрын
#awesome
@varunsheth1003
@varunsheth1003 Жыл бұрын
Nice explanation Sir !!!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Varun ❤️❤️❤️
@shubhamsandanshiv515
@shubhamsandanshiv515 Жыл бұрын
Greattttt
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Shubham ❤️❤️❤️
@danianiazi8229
@danianiazi8229 Жыл бұрын
I have just started question solving on leetcode and learning dsa but always I dont come up with approach like this
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Dania, it’s totally normal. We all get stuck. But with consistency and practice, we all can do great. Let’s do it 💪💪💪
@YashSinghal
@YashSinghal Жыл бұрын
Can we use the totalSum function to find the subTreeSum? this way we can keep find as void fn and no need to return
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Yash, Can you share the code. Sorry i am not able to follow 😇
@YashSinghal
@YashSinghal Жыл бұрын
@@codestorywithMIK I was thinking the wrong thing. It doesn't work
@danianiazi8229
@danianiazi8229 Жыл бұрын
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { int totalSum=0; int maxProd = 1; public int maxProduct(TreeNode root) { int totalSum = getTotalSum(root); int sum = findProduct(root); System.out.println("sum"+sum); return maxProd; } public int getTotalSum(TreeNode root){ if(root==null) return 0; int sum = root.val + getTotalSum(root.left)+getTotalSum(root.right); return sum; } public int findProduct(TreeNode root){ if(root==null) return 0; int subTreeSum = root.val + findProduct(root.left) + findProduct(root.right); int remainingSum = totalSum - subTreeSum; System.out.println(subTreeSum); System.out.println(remainingSum); System.out.println(remainingSum*subTreeSum); maxProd = Math.max(maxProd,remainingSum*subTreeSum); return subTreeSum; } } The same approach not working in java
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I think you made some small mistakes. I have corrected your code. It works now : class Solution { int totalSum=0; int maxProd = 0; public int maxProduct(TreeNode root) { totalSum = getTotalSum(root); findProduct(root); return maxProd%(1000000007); } public int getTotalSum(TreeNode root){ if(root==null) return 0; int sum = root.val + getTotalSum(root.left)+getTotalSum(root.right); return sum; } public int findProduct(TreeNode root){ if(root==null) return 0; int subTreeSum = root.val + findProduct(root.left) + findProduct(root.right); int remainingSum = totalSum - subTreeSum; maxProd = Math.max(maxProd,remainingSum*subTreeSum); return subTreeSum; } }
@yashchamoli8695
@yashchamoli8695 Жыл бұрын
good evening bhaiya can you plz provide the solution of 2049 leetcode as i am not able to understand how to make a tree from an array it is similar to today's question I would be thankful of you 🙏🙏🙏🙏🙏🙏
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Yash, Noted. I will soon add it Thanks for watching ❤️❤️❤️
@ayushivishwakarma9101
@ayushivishwakarma9101 5 ай бұрын
Bahiya maxp ko max product mai declare Kiya hai tho wo find k baad update kese hoga
@harsh.jain22
@harsh.jain22 Жыл бұрын
class Solution { public long maxProd = 0; public long totalSum; private static final int MOD = 1000000007; public int totalTreeSum(TreeNode root){ if(root == null) return 0; int LSum = totalTreeSum(root.left); int RSum = totalTreeSum(root.right); return LSum + root.val + RSum; } public int findingMax(TreeNode root){ if(root == null) return 0; int LSum = findingMax(root.left); int RSum = findingMax(root.right); int currSum = LSum + root.val + RSum; long prod = currSum * (totalSum - currSum); maxProd = Math.max(maxProd, prod); return currSum; } public int maxProduct(TreeNode root) { totalSum = totalTreeSum(root); findingMax(root); return (int)(maxProd % MOD); } }
@shashwatchawla
@shashwatchawla Жыл бұрын
Why i am getting TLE for last two testcases , I also did it same way , can you please tell me what I am doing wrong ? class Solution { public: long long int sumSubtree(TreeNode* root,long long int &sum) { if(root==NULL) return 0; sum+=root->val; sumSubtree(root->left,sum); sumSubtree(root->right,sum); return sum; } long long int solve(TreeNode* root,long long int overAllSum) { if(root==NULL) return maxProd; long long int sum=0; long long int sumLower= sumSubtree(root,sum); long long int sumUpper = overAllSum-sumLower; long long int prod = sumLower * sumUpper; maxProd= max(maxProd ,prod ); solve(root->left,overAllSum); solve(root->right,overAllSum); return maxProd; } long long int maxProd=INT_MIN; int maxProduct(TreeNode* root) { long long int sum=0; long long int overAllSum = sumSubtree(root,sum); long long int l= solve(root->left,overAllSum); long long int r = solve(root->right,overAllSum); int ans = max(l,r) % (1000000007); return ans; } };
@shashwatchawla
@shashwatchawla Жыл бұрын
Is it becz i am computing top to down and recomputing subtreesum everttime i come one level down ? :(
@codestorywithMIK
@codestorywithMIK Жыл бұрын
You have travelled the whole tree 3 times. May be that’s why
@codestorywithMIK
@codestorywithMIK Жыл бұрын
SumSutree Solve Solve
@codestorywithMIK
@codestorywithMIK Жыл бұрын
You can do it in 2 calls only. Like I mentioned in the video
@shashwatchawla
@shashwatchawla Жыл бұрын
Yeah sir , thanks alot :)
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 30 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 31 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН
C++ Tutorial for Beginners - Full Course
4:01:19
freeCodeCamp.org
Рет қаралды 12 МЛН
Machine Learning for Everybody - Full Course
3:53:53
freeCodeCamp.org
Рет қаралды 8 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 30 МЛН