Perfect squares | Dynamic programming | Legendre's theorem | Leetcode

  Рет қаралды 42,790

Techdose

Techdose

Күн бұрын

This video explains an important programming interview problem which is to find the minimum number of sqaures which can be added to form a given
number N.We can solve this problem by various methods but the simple recursive method will take exponential time.I have shown 3 methods to solve
this problem.The first method is by using recursion with memoization to remove unnecessary recursion calls.The second method is by using Dynamic Programming tabulation method.The third method is based on Legendre's 3-square theorem.This method gives the best time complexity of just O(sqrt(N)).At the end of the video, i have shown the code walk-through.
CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
=================================================================
CODE LINK: gist.github.co...

Пікірлер: 143
@abhijeetkumar2204
@abhijeetkumar2204 4 жыл бұрын
1 hour ago I did it using dp.But I am addicted to your explanation so I came here.
@techdose4u
@techdose4u 4 жыл бұрын
Nice :)
@abhijeetkumar2204
@abhijeetkumar2204 4 жыл бұрын
@@techdose4u can you make video on how to use leetcode for interview preparation.thank u
@abhijeetkumar2204
@abhijeetkumar2204 4 жыл бұрын
Also please make a video on how to to explain a problem in interview.
@techdose4u
@techdose4u 4 жыл бұрын
Good idea I will make this first.
@techdose4u
@techdose4u 4 жыл бұрын
Yea this is also good
@Official-tk3nc
@Official-tk3nc 4 жыл бұрын
Tushor Roy, Back to Back SWE and Tech Dose -----------are combo pack for getting dream company
@techdose4u
@techdose4u 4 жыл бұрын
☺️
@hellorvce
@hellorvce 4 жыл бұрын
excellent explanation ...pls keep it up.....generally coders r not good at explaining what they code.....but u r a gem man!! hatsoff
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@marcousosewelle5501
@marcousosewelle5501 4 жыл бұрын
Dude. If I ever make it into a top 5 tech company then I'll give you my first pay check. Serious. You are AMAZING!!!!!!!!!!
@r3xxxx
@r3xxxx Жыл бұрын
damn guy did you?
@eshanprasad3594
@eshanprasad3594 Жыл бұрын
Its been 3years since you posted this comment, I'm curious to know did you make it?
@AkshayKumar-xh2ob
@AkshayKumar-xh2ob 3 жыл бұрын
Your solutions are always very unique, I'm curious how you gained this knowledge. Bcoz you either know this approach or you don't. There is no way to derive or logically build it.
@Tensor08
@Tensor08 3 жыл бұрын
Bhai Mazza aa gya 🤞👌👌👌👌👌
@techdose4u
@techdose4u 3 жыл бұрын
😊
@watchforabhay
@watchforabhay 3 жыл бұрын
There are two aspects to such problems. The logic of the problem - which is real classy here in your video, clean and simple. The other aspect being that of implementation - the crux is given -- meaning the for loop. If you could really spell out how each case connects with the code (code flow) -- that will be super awesome and will be helpful for newbies . But overall great solution and explanation. Very helpful and really thankful
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@aakashparmar560
@aakashparmar560 3 жыл бұрын
Best video explanation. You got a new subscriber here.!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@mohanvamsi007
@mohanvamsi007 4 жыл бұрын
Your explanation is so elegant make it very simple to understand the solution
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@radhu8
@radhu8 3 жыл бұрын
LOL , Now I want to try only those questions on Leetcode for which you have a video! Your explanations are awesome! Please add more Medium and Hard level leetcodes!
@techdose4u
@techdose4u 3 жыл бұрын
Haha....one day I shall cover all of leetcode 😀
@deepaligarg2978
@deepaligarg2978 3 жыл бұрын
Superbly explained. Thank you so much for making this video.
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@awfulprogrammer619
@awfulprogrammer619 2 жыл бұрын
Keep Upload! You really helping a lot. KEEP SHINING too
@gurnoorsingh2954
@gurnoorsingh2954 4 жыл бұрын
Great explanation sir Aur koi problem channel itna clear nhi samjhata
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@venothanand91
@venothanand91 4 жыл бұрын
Kudos to Your thought process. Clean and simple.
@techdose4u
@techdose4u 4 жыл бұрын
😁
@alphagrowth3327
@alphagrowth3327 3 жыл бұрын
God level explanation. Great work man 🙌
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
@SKovac
@SKovac 3 жыл бұрын
Excellent explanation!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks ☺️
@chandrabhusanyadav5741
@chandrabhusanyadav5741 2 жыл бұрын
Excellent 👍
@srinivasakumaratmakuru293
@srinivasakumaratmakuru293 Жыл бұрын
🎯 Key Takeaways for quick navigation: 🧮 The *video discusses solving a mathematical problem related to perfect squares and dynamic programming.* 💡 It *aims to find the number of perfect square sums that can be obtained from a given list of numbers.* 📝 Legendre's *theorem is mentioned as part of the solution strategy.* 🎯 The *goal is to achieve a minimum of 500 distinct sums.* 🤔 The *video hints at applying concepts like Indian cuisine to illustrate the problem-solving process.* 📚 It *emphasizes the importance of subscribing to the channel for more content and updates.* 🔍 The *video encourages viewers to consider various factors and strategies in problem-solving.* 📈 It *suggests that by understanding key concepts, viewers can tackle similar problems effectively.* Subscribing to *the channel is emphasized multiple times throughout the transcript.* The content *discusses various mathematical concepts, including squares, multiples, and divisibility.* There's a *mention of the importance of subscribing to receive updates and new content.* The transcript *includes some numerical examples and calculations related to mathematical problems.* Different methods *and strategies for solving problems are mentioned.* The transcript *also mentions the representation of numbers using different techniques.* It emphasizes *the importance of understanding and verifying solutions.* There are *reminders to subscribe to the channel for more content and updates.* Made with HARPA AI
@chandrakantasaini6438
@chandrakantasaini6438 4 жыл бұрын
the way you explain, it is awesome. really impressive.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@oqant0424
@oqant0424 3 жыл бұрын
thanks a lotttttttttt u explained it so well🤩
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😊
@ArifulIslam-im7wr
@ArifulIslam-im7wr 4 жыл бұрын
Thanks...Sir, You Always Teaching The Best Methods.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@bookalicious9849
@bookalicious9849 3 жыл бұрын
Thank You ! you are the life savior of mine
@lokeshtech3319
@lokeshtech3319 4 жыл бұрын
Very nice explanation
@abhilashpatel6852
@abhilashpatel6852 2 жыл бұрын
Thank you for the explanation. This question was giving me nightmare
@VinayKumar-ze2ww
@VinayKumar-ze2ww 2 жыл бұрын
Great solution
@chetanpatteparapu7600
@chetanpatteparapu7600 4 жыл бұрын
This video is gold!
@techdose4u
@techdose4u 4 жыл бұрын
👍🏼
@4747surya
@4747surya 3 жыл бұрын
Can also be solved like coin change problem of leetcode least number of digits required to add up to a given number make a array from i=1 to i * I
@mrityunjaygiri3397
@mrityunjaygiri3397 2 жыл бұрын
wonderfully explained
@kajalkukreja694
@kajalkukreja694 2 жыл бұрын
Such a clear explanation. Great work 👍👍👍👍
@sarahmimi5938
@sarahmimi5938 3 жыл бұрын
Thank you so much! A real explanation!
@maths_with_fun8592
@maths_with_fun8592 4 жыл бұрын
Nice explanation. Amazing
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@visheshyadav2683
@visheshyadav2683 3 жыл бұрын
Your explanation is the best bro ,keep it up❤️
@techdose4u
@techdose4u 3 жыл бұрын
Thanks ☺️
@sayanchowdhury3776
@sayanchowdhury3776 4 жыл бұрын
sir thank you so much ,great explanation......the idea behind this is superb.....
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@Rishit.Chaudhary
@Rishit.Chaudhary 4 жыл бұрын
For the memoization and DP method, you can start in a greedy fashion and start with perfect squares just smaller than N every function call. Something like: for(int i = sqrt(N); i*i as i is an int type and i*i = 1024 so the next call would be min(ans, 1+solve(N-i*i)) => min(ans, 1+solve(1025-1024)) This way the recursion would get the answer faster, but we have to use the branch and bound as well to prune the state space tree so that we do not explore it completely and waste time. We use branch bound technique here where we check if we are subtracting more numbers than what the current best answer did and if so then we can stop the recursion then and there and try some other lower value instead. This way we do not explore branches that will surely not give us the optimal answer, i.e. least number of subtractions for getting N.
@garimakumari4346
@garimakumari4346 3 жыл бұрын
woow man..thanks
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😀
@amanrai9880
@amanrai9880 4 жыл бұрын
Dude you never seize to amaze me!!
@techdose4u
@techdose4u 4 жыл бұрын
😁
@saravanan925
@saravanan925 4 жыл бұрын
What an explanation!!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
Really very interesting and amazing session 😊☺️.. thank you sir to make daily such a helpful video 😊☺️..
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@sangamsamdarshi4634
@sangamsamdarshi4634 Жыл бұрын
it would help if you add chapters to your videos, btw explanation is great
@paragroy5359
@paragroy5359 3 жыл бұрын
Nice explanation sir....doing a really great job thanks a lot for the video..really amazing..sir as we are using sqrt function in the for loop so don't you think the time complexity should be o(sqrt(n)*log(n))
@avikmallick2493
@avikmallick2493 3 жыл бұрын
wow...the last method was amazing..just wanna know, should we know these type of theroems before or we just get to know these theroems when these type of problems arise
@techdose4u
@techdose4u 3 жыл бұрын
Learn & apply :)
@snehal462
@snehal462 4 жыл бұрын
man your solutions are amazing!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks bro :)
@anmolwadali9227
@anmolwadali9227 4 жыл бұрын
really great explanation sir
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@suharajsalim4549
@suharajsalim4549 4 жыл бұрын
Thanks a lot!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@liaodaniel1684
@liaodaniel1684 2 жыл бұрын
Excuse me Sir, are the two points at 19:57 the reasons why 13 can be used to solve the number 52? Thank you for your amazing videos!
@rajatbudania6181
@rajatbudania6181 4 жыл бұрын
very well explained :)
@kbhargavi4400
@kbhargavi4400 4 жыл бұрын
Tq for ur efforts sir! 🙏🙏
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
Is it correct to think this way? 1) If number is perfect square, it will be sum of 1 square 2) If number is a Pythogorean hypotenuse, it will be sum of 2 squares. It also need not be pythogorean hypotenuse :) 3) Otherwise if it is not of the form (4^a)*(8*b +7) then it is the sum of 3 squares 4) otherwise it is the sum of 4 squares
@ridimamittal4064
@ridimamittal4064 4 жыл бұрын
THANK YOU SIR !!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@adityaagarwal1650
@adityaagarwal1650 4 жыл бұрын
bhaiya how much total time do you invest in these solutions , ranging from finding different methods to recording them nicely ? :):):) Thanks for your hard work .
@techdose4u
@techdose4u 4 жыл бұрын
Bro...you can only know it when you see me live. It requires a lot of effort to put video for a good question :) Thanks for appreciating my efforts.
@skystone1000
@skystone1000 4 жыл бұрын
@@techdose4u Yes, I know it takes a lot of effort to do these things, thank you for your detailed explanation
@TheBatterHalf
@TheBatterHalf 4 жыл бұрын
Great video! Thank you! What is the time and space complexity for recursion and memoization?
@optimizedvishu7754
@optimizedvishu7754 2 жыл бұрын
in line no.8 you are changing n , by dividing it by 4 , and using same value of n every where in the last code sir please explain if is it working ?
@jatinthakwani5370
@jatinthakwani5370 4 жыл бұрын
Thank you so much
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@abhaysahoo7652
@abhaysahoo7652 3 жыл бұрын
sir what is the software you use for writing on the screen?
@rakshith3547
@rakshith3547 4 жыл бұрын
Baap of DP!
@techdose4u
@techdose4u 4 жыл бұрын
🙏
@zuojimmy1198
@zuojimmy1198 3 жыл бұрын
thanks a lot
@AbhijeetNayak-connect
@AbhijeetNayak-connect 4 жыл бұрын
Great videos, reduce the usage of ‘lets us say’, ‘hence’, ‘because’, ‘so’.
@techdose4u
@techdose4u 4 жыл бұрын
That's the problem with me. I know I use many so's therefore etc. I dunno how to get rid of this 😅 Any suggestion?
@AbhijeetNayak-connect
@AbhijeetNayak-connect 4 жыл бұрын
@@techdose4u Practice is the only way to get rid of these filler words. You've the videos to take notes of how many times you're saying these, that will give you an idea. Then, consciously you have to suppress these word. Sounds easier said than done :)
@yitingg7942
@yitingg7942 4 жыл бұрын
Your explanation is amazing!! Can you also do one for lc 91 Decode Ways please ?
@JgNt3981
@JgNt3981 Жыл бұрын
Sir, How to develop probelm solving skill ?
@myvinbarboza3038
@myvinbarboza3038 4 жыл бұрын
Thanks :)
@nishantjawla4085
@nishantjawla4085 2 жыл бұрын
For the method 3 bcz of while(n%4 == 0) th TC should be O(n) ?
@kimpma7380
@kimpma7380 4 жыл бұрын
why we need to create the int dp [n+1] instead of dp[n] when we use the recursion with memorization? Thanks
@babycoder-shantanusaha5218
@babycoder-shantanusaha5218 3 жыл бұрын
13=3*3+2*2 13=9+4 2 is the minimum
@crimsoncad3230
@crimsoncad3230 4 жыл бұрын
Can you please help me with dynamic programming??? I want to know how to start with dynamic programming. Easy to Medium. Just like you do for recursion when you are discussing a easy Tree question. Do it for DP as well.
@techdose4u
@techdose4u 4 жыл бұрын
Got it bro. Actually in geeksforgeeks, you can find under DP, problems are sorted hardness wise.
@crimsoncad3230
@crimsoncad3230 4 жыл бұрын
@@techdose4u Thanks bro. It was very helpful. But just when you are solving any DP problem, please mention something like these in the video: "This is a good problem to understand DP" OR "This problem might be a bit difficult for those people who are starting with DP" OR "This is an easy/intermediate/high level DP problem"
@exodus5948
@exodus5948 4 жыл бұрын
@@crimsoncad3230 i suggest you to understand 0-1 knapsack problems. I have some standard 7-8 questions and most of the new dp questions are also an extended form of this as well.
@kavitameena67
@kavitameena67 4 жыл бұрын
Which tool do you use for drawing and explaining ?
@tusharsharma5109
@tusharsharma5109 4 жыл бұрын
Bro i read in your recent post that you will stop this Leetcode monthly series from July , please don't your explanation of these problems are just amazing. Even if I come up with a solution of my own then too i watch your video because always i learn something new :)
@techdose4u
@techdose4u 4 жыл бұрын
I know its painful but still I had to take this decision. It will be more helpful to teach topicwise rather than random problems :)
@AkshayKumar-xh2ob
@AkshayKumar-xh2ob 3 жыл бұрын
JAVA DP tabulation solution: class Solution { public int numSquares(int n) { int dp[] = new int[n+1]; for(int i=0; i
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@ThePaullam328
@ThePaullam328 Жыл бұрын
I'm confused about how you derived n % 8 == 7 from N / 4 ^ a = 8b + 7?
@vineetsharma4635
@vineetsharma4635 4 жыл бұрын
What's the time complexity of recursive solution? Moreover, how to define a recurrence relation for it?
@abhinavshukla5164
@abhinavshukla5164 3 жыл бұрын
Meomoization code cplexity is o(n)2
@SwetaKumari-uv4ul
@SwetaKumari-uv4ul 4 жыл бұрын
Please upload video on egg dropping problem dynamic programming
@techdose4u
@techdose4u 4 жыл бұрын
Sure
@darshantank554
@darshantank554 4 жыл бұрын
First view🥳🤗
@techdose4u
@techdose4u 4 жыл бұрын
😁
@rishabhgupta7976
@rishabhgupta7976 4 жыл бұрын
make video on wildcraft matching please
@rahulagarwal8059
@rahulagarwal8059 4 жыл бұрын
👍🏻
@techdose4u
@techdose4u 4 жыл бұрын
👍
@rohithkumartangudu4664
@rohithkumartangudu4664 4 жыл бұрын
Bro please reduce the ads. It is disturbing while watching video.
@techdose4u
@techdose4u 4 жыл бұрын
Sure.
@rohithkumartangudu4664
@rohithkumartangudu4664 4 жыл бұрын
@@techdose4u thank you
@techdose4u
@techdose4u 4 жыл бұрын
@@rohithkumartangudu4664 DONE
@rohithkumartangudu4664
@rohithkumartangudu4664 4 жыл бұрын
@@techdose4u thank you bro. Are you telugu guy??
@techdose4u
@techdose4u 4 жыл бұрын
No 😅
@josemarcano5136
@josemarcano5136 4 жыл бұрын
why ans=Math.min(ans, 1 + solve(n-i*i)); //why this formula. how did you come up with this formula? how can we derive this formula in 20 mins?
@techdose4u
@techdose4u 4 жыл бұрын
This is not a formula. I showed you, that this is a breakpoint generation. You need to create breakpoints at Perfect Square intervals. This is the entire concept in this problem.
@abhijeetkumar2204
@abhijeetkumar2204 4 жыл бұрын
@@techdose4u this question was somewhere similar to coin exchange problem
@subham-raj
@subham-raj 4 жыл бұрын
I came up with the dp solutions, but the math tho :(
@vikramsingla2311
@vikramsingla2311 4 жыл бұрын
Won't this solution also work if we ignore "Time Limit Exceeded" clause shown on leetcode. public int numSquares(int n) { int count=0; while(n>=0) { int a= (int)Math.sqrt(n); count++; n=n-(a*a); } return count; }
@DeepakKumar-nu6zo
@DeepakKumar-nu6zo 4 жыл бұрын
No see when u do it for a no like 12 in first case u reduce ur no to 3 2nd case to 2 3rd case to 1 ,,oh then wait a minute no remains 1 all the time infinite loop.......
@vipinshanmugam3036
@vipinshanmugam3036 2 жыл бұрын
i don't understand why we are taking minimum in recursive approach, someone help me! P.S: I'm not from a CS stream🙃
@manjeshsingh3002
@manjeshsingh3002 4 жыл бұрын
hadd h YAAR..koi videos dekhe ya tmhare ads?????
@noname-ut5rw
@noname-ut5rw 4 жыл бұрын
Bad
@ankitMalviya152
@ankitMalviya152 3 жыл бұрын
very good explanation
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
Unique Paths | Dynamic programming | Leetcode #62
14:00
Techdose
Рет қаралды 62 М.
Minimum edit distance | Dynamic programming | Backtracking
28:52
Lecture 112: Perfect Squares Problem || DP Series
20:38
CodeHelp - by Babbar
Рет қаралды 62 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
Perfect Squares - Dynamic Programming - Leetcode 279 - Python
15:12
C can do this too and it's faster than Python
2:09:48
Tsoding Daily
Рет қаралды 21 М.
Trapping Rainwater Problem | Leetcode #42
34:12
Techdose
Рет қаралды 101 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 168 М.
950. Reveal Cards In Increasing Order | Queue | Simulation
14:27
Aryan Mittal
Рет қаралды 7 М.