Split Array Largest Sum - Leetcode 410 - Python

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

NeetCode

NeetCode

Күн бұрын

Пікірлер: 94
@NeetCode
@NeetCode 2 жыл бұрын
Python - github.com/neetcode-gh/leetcode/blob/main/410-Split-Array-Largest-Sum.py
@nicolaswolyniec1354
@nicolaswolyniec1354 Жыл бұрын
Link not working 😢
@devdev19
@devdev19 2 жыл бұрын
Whenever Im stuck on a problem, I check if NeetCode has a solution available and like so many times before you came through. Thank you NeetCode
@chaoluncai4300
@chaoluncai4300 2 жыл бұрын
Indeed. I swear Im not gonna waste 10xtime on lc official solutions anymore, neetcode is the saviour!!
@niteshmanem1997
@niteshmanem1997 2 жыл бұрын
Neet has finally broken out of "Hard Level" brain area and breached into this new area called the "Crackhead zone". He is the first of mankind to reach this point of the brain. I have full confidence that Neet will continue such successful expeditions through the Crackhead zone as he continues to solve more cracked leetcode problems
@numberonep5404
@numberonep5404 2 жыл бұрын
Lovely explanation as usual! You know it's an actual hard problem when the DP solution is the easiest one to come up with :p
@theantonlulz
@theantonlulz 2 жыл бұрын
Other than the weird 37 instead of 42 typo this is a spectacular explanation of the algorithm. Strongly doubt that everyone on LC who post this exact same algorithm actually thought of it themselves.
@李順安-p3x
@李順安-p3x 2 жыл бұрын
In the DP approach, if you can reduce the time doing "sum(nums[i:])" to O(1) by using some technique like subtraction of two prefix sums, you can actually AC (with poor runtime)
@dpsingh_287
@dpsingh_287 2 жыл бұрын
I did this lol. made a prefix sum array and it ac'd after adding that "micro optimization" mentioned in the video
@chaoluncai4300
@chaoluncai4300 2 жыл бұрын
w
@iscoto4914
@iscoto4914 Жыл бұрын
@@dpsingh_287 can u share the code please?
@ketangupta1910
@ketangupta1910 2 жыл бұрын
the only way to guess that this is a binary search problem is that in this week leetcode has given all binary search questions🤣🤣🤣
@abhimanyuambastha2595
@abhimanyuambastha2595 6 ай бұрын
This problem is exactly same as Leetcode 1011. The same code also works for this. Its just a lot difficult to understand how this problem is similar to 1011
@akshitthakur5179
@akshitthakur5179 2 жыл бұрын
binary search approach of this problem is similar to book allocation problem pattern, just in case someone is wondering about the intuition
@vishalvanpariya1466
@vishalvanpariya1466 Ай бұрын
subarray + 1
@nishantingle1438
@nishantingle1438 2 жыл бұрын
I converted that backtracking solution to a recursive function & then memoized it to make a dp solution & it worked with 10% faster & 90% space efficient. Variation of Matrix Chain Multiplication.
@shengzhaolei5500
@shengzhaolei5500 2 жыл бұрын
I think that initializing subarray as 1 is better, since we can consider that we create an empty subarray first and then add the item into the subarray; if we initialize as 0, then you need to add 1 as the sum of last subarray is less than largest and you have to count it.
@vdyb745
@vdyb745 2 жыл бұрын
Mindbender !!! Would have never realized it is a binary search problem !!! Simply Brill !!!!! 👏👏👏🙇‍♂
@zaidshaikh2536
@zaidshaikh2536 2 жыл бұрын
Glad you made this video. This problem is indeed Unique in Itself.
@NeetCode
@NeetCode 2 жыл бұрын
True, it's definitely not obvious
@vijethkashyap151
@vijethkashyap151 3 күн бұрын
❤ Just gratitude man! Best channel ever!
@koeber99
@koeber99 2 жыл бұрын
The plus one is needed in canSplit function because leftover in curSum needs to be in it's own "subarray". Therefore " if ( curSum>0 ) subArrays++; " is needed in canSplit function. Then finally line because (subarray +1
@Kidpunk98
@Kidpunk98 Жыл бұрын
Is it just me, or is 37 the wrong value for initial right? The total sum of numbers is 42.
@SweetDarkViolet
@SweetDarkViolet 2 жыл бұрын
Ohhh here it comes. Last daily challenge of March.
@NeetCode
@NeetCode 2 жыл бұрын
Yeah, this coming sunday I will introduce something cool for the channel - you heard it here first :)
@darshanputtaswamy3199
@darshanputtaswamy3199 2 жыл бұрын
What's that ?
@mahendar7733
@mahendar7733 Жыл бұрын
If your code is not working then try returning low after the if else condition at last it was cut down from the video. Returning should solve your problem
@JijaChad-os8mo
@JijaChad-os8mo 2 жыл бұрын
Thank you so much bro for your explanations. But with your help, I am gaining some confidence in solving hard problems.
@nitaikodkani
@nitaikodkani Жыл бұрын
was stuck on this one, thanks a lot for explaining it so well
@ytsrlee9224
@ytsrlee9224 2 жыл бұрын
Nice video! For some moment, I came across the searching space, but stuck in the meaning of mid. Thanks for explain the mid and the greedy method.
@soumikkarmakar7433
@soumikkarmakar7433 2 жыл бұрын
Similar to "Allocate minimum number of pages" problem.
@nikhilsingh2184
@nikhilsingh2184 2 жыл бұрын
Into the first 4 min of video. I come up with this binary search . But was struggling with conditions to check the largest number lol
@shrimpo6416
@shrimpo6416 Жыл бұрын
hi everyone, I'm the dum dum that uses backtrack without cache 🤣 I'm so glad I learned the bin search solution!
@kareni7572
@kareni7572 5 ай бұрын
Thanks for the dp recursive solution!
@sushantrocks
@sushantrocks 2 жыл бұрын
similar to fair workload - there we can get the relation to bin search
@hkanything
@hkanything 2 жыл бұрын
Where is the 37 comes from? 7+2+5+10+18 =42
@yunshuwang5078
@yunshuwang5078 2 жыл бұрын
I double count and confused myself
@simpendatahapefentira1280
@simpendatahapefentira1280 2 жыл бұрын
Here the count: 18(min , max that we can get in 1 elemen array) .......... 42 (sum for max ). So between 18 and 42 , we find the mid of 27. And 37 is sum of adjusting right grup elemen array
@塩酸田代
@塩酸田代 2 жыл бұрын
37 definitely comes from an incorrect math or typo. I add print() in his python code and run it with the example he uses in this video. Then I got the first round of (l, r, mid) is (18, 42, 30)
@varunsen2802
@varunsen2802 2 жыл бұрын
Thank you for this solution.
@NeetCode
@NeetCode 2 жыл бұрын
No problem!
@albertchen5501
@albertchen5501 2 жыл бұрын
I come up with a break even approach. Let me debug and see if it’s more efficient 🙂
@lambar0
@lambar0 Жыл бұрын
Neat Code … simple and precise
@neelmehta9092
@neelmehta9092 2 жыл бұрын
The person who made this problem is evil
@ritikjain307
@ritikjain307 2 жыл бұрын
Of topic question, in python is it better to use nested functions or not. How to define different functions and use them.
@theearthish
@theearthish 2 жыл бұрын
Was waiting for this lol 100% crackhead intuition needed. Amazing explanation as always 🙌
@josephchen1344
@josephchen1344 2 жыл бұрын
Hi NeetCode, What if the question becomes: split an array into m non-empty "subsets" (doesn't need to be continuous) and minimize the maximum sum among these subsets? Can you think of any faster solution than backtracking through all possible combination?
@nameless2850
@nameless2850 2 жыл бұрын
May be solving after sorting would do?
@criticstar123
@criticstar123 7 ай бұрын
I used brute force method got the ans in the end but it is not optimal algorithm does it matter in interviews
@soumyadeepdas1536
@soumyadeepdas1536 Жыл бұрын
It's basically the painters partition problem
@Chirayu19
@Chirayu19 Жыл бұрын
Can we build a prefix sum array and use it somehow to solve the problem greedily or binary search on the same array?
@shikshamishra5668
@shikshamishra5668 Жыл бұрын
can we do by keep target=sum(nums)//k and finding the split?
@AniketSingh-mt6zr
@AniketSingh-mt6zr Жыл бұрын
classy explanation
@HardeepSingh-pi2hr
@HardeepSingh-pi2hr 2 жыл бұрын
Am I missing something or is the code working without any return statement inside splitArray method? We should be returning res right?
@NeetCode
@NeetCode 2 жыл бұрын
Good catch, your right - the return statement was cutoff of the screen. Sorry for confusion, added the entire code in a comment above.
@HardeepSingh-pi2hr
@HardeepSingh-pi2hr 2 жыл бұрын
Ohh not a problem... I thought I was tripping xD
@HardeepSingh-pi2hr
@HardeepSingh-pi2hr 2 жыл бұрын
And thanks for your hard work. You have a new fan in me 🙌
@nishantingle1438
@nishantingle1438 2 жыл бұрын
The Optimal solution is a pretty standard variation of Binary Search. Check Allocate Pages from GeeksForGeeks/Aditya Verma's KZbin video or LC 1011.
@superheroherohero
@superheroherohero 2 жыл бұрын
Thank you!
@rongrongmiao3018
@rongrongmiao3018 2 жыл бұрын
Oh well I was just looking at this problem today
@DavidDLee
@DavidDLee 5 ай бұрын
1. Missing return: code will not run 2. 16:06 missed the mark on explaining the logic back in 10:03
@koeber99
@koeber99 Жыл бұрын
Why is the canSplit function ""return (subarry +1
@prrnnv2
@prrnnv2 Жыл бұрын
you can always split a subarray into further smaller subarrays without violating the maximum condition, since there is no upper limit on minimum sum of the subarrays
@i8you
@i8you 10 ай бұрын
hey @NeetCode awesome explanation but i was wondering without return how the answer is returning .. don't we need to write return res ??
@Zifox20
@Zifox20 2 жыл бұрын
Easy Problem: How many videos do I need before you become my favourite youtuber?
@yan-vn5oy
@yan-vn5oy 2 жыл бұрын
good explanations as aways
@tenzin8773
@tenzin8773 5 ай бұрын
If i'm asked this question in the interview, I am beyond cooked
@AbdulAziz-fp3hz
@AbdulAziz-fp3hz 2 жыл бұрын
Is this a very hard problem for someone who started DSA about 2 weeks ago?
@uditsharma5688
@uditsharma5688 2 жыл бұрын
When the backtracking solution passed , was it faster than the binary search?
@NeetCode
@NeetCode 2 жыл бұрын
Not even close, binary search is much more efficient
@italianpipes1849
@italianpipes1849 Жыл бұрын
Grazie mille!
@jugalparulekar661
@jugalparulekar661 2 жыл бұрын
Just wanted to bring the viewers' attention to the missing "return res" statement at the end of the splitArray function.
@jackie0315
@jackie0315 2 жыл бұрын
I guess I dont understand how in the "canSplit" function, you can be so greedy? How can you just add numbers from left to right as the array elements? What if the optimal members of the array elements such the sum is less than target is not from left to right?
@anybody413
@anybody413 2 жыл бұрын
Thanks!
@saurabhkumarsrivastava6034
@saurabhkumarsrivastava6034 3 ай бұрын
I think that it's the same as book allocation problem
@khanhquoc5352
@khanhquoc5352 2 жыл бұрын
i dont really understand that, in canSplit fuction, to compute sub sum, why we currentSum+= n instead of curSum += num[i]. Thanks for answering me.
@infiniteloop5449
@infiniteloop5449 Жыл бұрын
3 months too late but n is num[I]. for n in nums is going through every value in nums
@lazydoner6552
@lazydoner6552 Жыл бұрын
Is this question similar to book allocation problem of ggg?
@snex-techprogrammer5110
@snex-techprogrammer5110 2 жыл бұрын
'return res' at the end
@sarbajitde2547
@sarbajitde2547 8 ай бұрын
How is the code working without any return statement?
@anthonyuccello1458
@anthonyuccello1458 2 жыл бұрын
Solution is O(crackhead)
@bchen1403
@bchen1403 2 жыл бұрын
this is madness
@sipwhitemocha
@sipwhitemocha 2 жыл бұрын
Could someone please help me how R = sum(nums) is initially 37??? 8:55
@infiniteloop5449
@infiniteloop5449 Жыл бұрын
Crack Head Problems would be an interesting KZbin List.
@joydeeprony89
@joydeeprony89 Жыл бұрын
why low = max(nums) ?
@yashwants18
@yashwants18 Жыл бұрын
Let say if m = len(nums) then what would be the answer? I guess max(nums)
@alps747
@alps747 2 жыл бұрын
I couldn't understand the explanation starting at 10:25 (kzbin.info/www/bejne/j4apZJKbd8mtqc0). Why having two subarrays is not a problem. what if m was 6 and there was no way to split the left subarray into 6 subarrays since it has only 5 elements? Could someone pls explain?
@kongzilla2897
@kongzilla2897 2 жыл бұрын
In constraints it is mentioned that m < nums.size()
@dokudenpa8207
@dokudenpa8207 2 жыл бұрын
Wait so this is pretty much identical to 1011. Capacity To Ship Packages Within D Days. How comes one is ranked as hard while the other is ranked as medium?
@bouzie8000
@bouzie8000 9 ай бұрын
Lol this is truly a crackhead solution. God help us all
@Shaaah_
@Shaaah_ Жыл бұрын
⬇Simple Code | TC: O( n*log( sum(nums) ) | SC: O( 1 ) int splitArray(vector& nums, int m) { int l=0,r=0,n=nums.size(); for(int i=0;i
@gokulrajr
@gokulrajr Жыл бұрын
Hi bro, 1712. Ways to Split Array Into Three Subarrays Could you please explain the binary search and sliding window approach of this leetcode problem...
@kaushalkumar-kj9uh
@kaushalkumar-kj9uh 2 жыл бұрын
this prob is exactly same :1011. Capacity To Ship Packages Within D Days
Binary Search - Leetcode 704 - Python
9:40
NeetCode
Рет қаралды 170 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
Can You Find Hulk's True Love? Real vs Fake Girlfriend Challenge | Roblox 3D
00:24
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 518 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,3 МЛН
BS 19. Painter's Partition and Split Array - Largest Sum
11:20
take U forward
Рет қаралды 115 М.
Find K Closest Elements - Leetcode 658 - Python
18:21
NeetCode
Рет қаралды 76 М.
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 444 М.
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2,1 МЛН