Integer Break - Dynamic Programming - Leetcode 343 - Python

  Рет қаралды 31,272

NeetCode

NeetCode

Күн бұрын

Пікірлер: 60
@NeetCode
@NeetCode 3 жыл бұрын
Let me know if you prefer drawing & code at the same time or separately!
@TheElementFive
@TheElementFive 3 жыл бұрын
Same time!
@yamibakura7491
@yamibakura7491 3 жыл бұрын
Doing it side by side gives an experience of how to do it in real interviews. Loved the video, Thanks for explanation!
@ThiagoTokarski
@ThiagoTokarski 3 жыл бұрын
same time
@ROFEL
@ROFEL 3 жыл бұрын
Same Time!
@joyride6062
@joyride6062 3 жыл бұрын
Same time
@hardboiledege
@hardboiledege 2 жыл бұрын
The reason your DP solution is slower than you expected is that you're doing double the work in your inner loop. Specifically, you can do `for i in range(1, num // 2):` instead of `for i in range(1, num):` because `for i in range(num // 2 + 1, num):` is essentially a repeat of the previous half.
@sandeepmourya8922
@sandeepmourya8922 Жыл бұрын
but then, there will be no overlapping subproblems right??
@justine-george
@justine-george Жыл бұрын
Brilliant
@irarose3536
@irarose3536 3 жыл бұрын
I'm so happy your chanel has sponsors💟
@CloseToNature-Palamou
@CloseToNature-Palamou 7 ай бұрын
For the inner for loop we can minimize the loop by only executing till num//2+1 to avoid duplicate calls / calculation .
@krzysztofsobocinski2207
@krzysztofsobocinski2207 2 жыл бұрын
Great explanation, although I preferred previous format - first explanation than code. I think the second loop could end earlier at num//2 + 1 because of symmetry.
@alonebeast5310
@alonebeast5310 2 жыл бұрын
yeah i was thinking the same thing and searching if someone ask that😃
@niallmandal2249
@niallmandal2249 2 жыл бұрын
@@alonebeast5310 its actually not symmetry. As the numbers get larger, the sum is made up solely of 2s and 3s.
@ytbcmt4686
@ytbcmt4686 3 жыл бұрын
Prefer the drawing first!!
@tomtom-wv3hc
@tomtom-wv3hc 3 жыл бұрын
Amazing content as usual :) Could you please draw first to get a wider picture of a problem. First, we need to create a mental image then we can code.
@sparshbatta7070
@sparshbatta7070 2 жыл бұрын
No, this method is better so that we may have a clear image of each and every step of the code and the whole intuition of why we need what we did at each stage!
@AR-yr5ov
@AR-yr5ov 3 жыл бұрын
This is a really confusing problem, I think understanding what they want is the hardest part.
@ngneerin
@ngneerin 2 жыл бұрын
There is a O(c) solution for this. Ceil(Num/2) × 2s (or 3×2s if remaining is odd)
@deepaksingh7042
@deepaksingh7042 4 ай бұрын
I dont agree. Please corect me if I am wrong Lets consider for n=8, Max product is = 2*3*3 = 18 But according to your approach it is Ceil(8/2)*2s = 2*2*2*2 = 16 which is less than optimal answer 18
@amateursoundz6262
@amateursoundz6262 2 жыл бұрын
After doing a ton of CP with C++, it is pleasant to look at python code
@CS_n00b
@CS_n00b 8 ай бұрын
Max product for a certain k would occur when the integers are as close to equal as possible
@ROFEL
@ROFEL 3 жыл бұрын
368: Largest Divisible Subset
@skyandshoe
@skyandshoe 2 жыл бұрын
Thank you for the solution !! It was very intuitive. I do have a question tho, if dp was an array rather than a dictionary it actually significantly slows down the time complexity. However, we are only obtaining the element so shouldn't the time of retrieval both be O(1) for both array and dictionary? Why the choice of dictionary?
@AhmadRizviYT
@AhmadRizviYT Жыл бұрын
if you implement with array you get collision error, but in dictionary you dont have to solve that
@shensean1784
@shensean1784 3 жыл бұрын
Thanks. I will init up to 3 😁
@srinadhp
@srinadhp 3 жыл бұрын
I prefer the old style. Again, a bit of confusing problem and the way you explained is great as usual. I still didnt get it, need to watch couple more times.
@NeetCode
@NeetCode 3 жыл бұрын
Appreciate the feedback sir!
@tempregex8520
@tempregex8520 5 ай бұрын
I actually am still finding it a little difficult to wrap my head around when input is n=2, and when the subproblem has n=2 i.e. why is dp[2]=2 ? i guess this is a very tricky edge case, i mean, I would have liked to handle it as a base case had the input number n =2, then i would return 1, but when 2 is a subproblem, then we would return 2 only and not 1. That part is a little confusing to me tbh.
@sushantrocks
@sushantrocks 3 жыл бұрын
Choices from [1,2,3,4,...,n-1] => coin change-esque?
@kentsang9376
@kentsang9376 3 жыл бұрын
Please make more dynamic programming videos!!!! They are so good
@alonalon8794
@alonalon8794 2 жыл бұрын
@NeetCode 4:05 why you can leave the 3 as it is? you must break it up into at least 2 elements. Or I'm wrong?
@berkeevrensevdi1788
@berkeevrensevdi1788 2 жыл бұрын
The question says that input must be broken into the sum of k positive integers, where k >= 2. When you break up 4 into two pieces as (1, 3), it means that you select 1 as the first number and selecting 3 as the second number without breaking into pieces obeys the rule of k >= 2. However, if the actual input were 3, then you can not select 3 directly because k would be equal to 1 and it would break the rule of k >= 2. So, you can always select the number itself, if the number is not the actual input.
@soumyajitchatterjee5822
@soumyajitchatterjee5822 Жыл бұрын
good explanation!!!@@berkeevrensevdi1788
@adityarajora7219
@adityarajora7219 7 ай бұрын
def integerBreak(self, n: int) -> int: maxBreakProductOfNum = list(range(n+1)) maxBreakProductOfNum[n] = 0 for i in range(n+1): for j in range(i): maxBreakProductOfNum[i] = max(maxBreakProductOfNum[i], maxBreakProductOfNum[j]*maxBreakProductOfNum[i-j]) return maxBreakProductOfNum[n]
@AsifIqbalR
@AsifIqbalR 3 жыл бұрын
Notification gang ✌
@jorkhachatryan317
@jorkhachatryan317 Жыл бұрын
I know it's about coding, but I found a math solution that is easier to understand and to code.
@AhmadRizviYT
@AhmadRizviYT Жыл бұрын
i saw the solution but i coded it in c++... so its not full cheating :)
@sidazhong2019
@sidazhong2019 Жыл бұрын
There is just no way to figure out the true DP solution directly. This problem is hard.
@bantyK
@bantyK 2 жыл бұрын
Drawing first please. I like to understand stuff first and then code it afterwards on my own. I think its more helpful that way
@dekumidoriya-qi2is
@dekumidoriya-qi2is 3 ай бұрын
this gives wrong answer for n == 2 and n == 3, as for 2, 1*1 = 1 and for 3 , 1*2 = 2 but for all other cases it works
@sangeethreddychejerla6527
@sangeethreddychejerla6527 3 жыл бұрын
Hello sir,I want help how can I learn logic for programming and I want learn python and I am self taught learner
@sergiobost4891
@sergiobost4891 3 жыл бұрын
How do you join the discord server
@NeetCode
@NeetCode 3 жыл бұрын
Can join here: discord.gg/ddjKRXPqtk
@jackie0315
@jackie0315 2 жыл бұрын
but your code here only breaks an integer n into two pieces? val = dp[i] *dp[num-i]? what if the optimal break has more than 2 pieces
@berkeevrensevdi1788
@berkeevrensevdi1788 2 жыл бұрын
dp[i] and dp[num-i] hold max values and they consist of at least one piece. For example, if the input were 8, dp will be {1 : 1, 2 : 2, 3 : 3, 4 : 4, 5 : 6, 6 : 9, 7 : 12, 8 : 18}. Here dp[8] = dp[6] * dp[2] = 18 -> dp[6] = dp[3] * dp[3] = 9 -> dp[3] = 3. So actually dp[8] is 3 * 3 * 2.
@shrinilthakkar4589
@shrinilthakkar4589 Жыл бұрын
Not the most optimized solution. It can be solved in constant time. Just try to break n into 2 equal halves. The product of those numbers will be the max possible products. If n is even, for example, n=6, max product = n/2*n/2 i.e. 3*3 = 9 If n is odd, for example, n=5, max product = int(n/2)*int(n-n/2) i.e. 2*3 = 6
@nehaagrawal25
@nehaagrawal25 Жыл бұрын
I don't agree. Let's say, If n is even, for example, n=8, n/2*n/2 i.e. 4*4 = 16 which is not equal to 18 (max product for 8).
@akhileshsonkar2061
@akhileshsonkar2061 3 жыл бұрын
First 😎
Matchsticks to Square - Leetcode 473 - Python
15:48
NeetCode
Рет қаралды 28 М.
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Delete and Earn - Dynamic Programming - Leetcode 740 - Python
19:09
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
Design Twitter - Leetcode 355 - Python
22:47
NeetCode
Рет қаралды 91 М.
Number of Dice Rolls with Target Sum - Leetcode 1155 - Python
20:20
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Climbing Stairs - Leetcode 70 - Dynamic Programming (Python)
10:54
343. Integer Break Leetcode | Leetcode daily challenge |
14:36
Runtime Error
Рет қаралды 1 М.
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН