Split Array Largest Sum - Leetcode 410 - Python

Split Array Largest Sum - Leetcode 410 - Python

NeetCode

2 года назад

89,425 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@nishantingle1438
@nishantingle1438 - 10.05.2022 17:46

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.

Ответить
@josephchen1344
@josephchen1344 - 18.05.2022 15:34

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?

Ответить
@bchen1403
@bchen1403 - 07.06.2022 16:21

this is madness

Ответить
@jugalparulekar661
@jugalparulekar661 - 03.07.2022 01:33

Just wanted to bring the viewers' attention to the missing "return res" statement at the end of the splitArray function.

Ответить
@snex-techprogrammer5110
@snex-techprogrammer5110 - 25.07.2022 14:07

'return res' at the end

Ответить
@theantonlulz
@theantonlulz - 25.07.2022 18:02

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.

Ответить
@shengzhaolei5500
@shengzhaolei5500 - 29.07.2022 10:16

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.

Ответить
@albertchen5501
@albertchen5501 - 03.08.2022 05:44

I come up with a break even approach. Let me debug and see if it’s more efficient 🙂

Ответить
@niteshmanem1997
@niteshmanem1997 - 10.08.2022 05:36

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

Ответить
@ytsrlee9224
@ytsrlee9224 - 11.08.2022 10:10

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.

Ответить
@dokudenpa8207
@dokudenpa8207 - 17.08.2022 18:54

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?

Ответить
@superheroherohero
@superheroherohero - 17.08.2022 23:03

Thank you!

Ответить
@koeber99
@koeber99 - 27.09.2022 01:52

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<=m).

Ответить
@jackie0315
@jackie0315 - 30.09.2022 03:05

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?

Ответить
@AbdulAziz-fp3hz
@AbdulAziz-fp3hz - 13.10.2022 15:27

Is this a very hard problem for someone who started DSA about 2 weeks ago?

Ответить
@kaushalkumar-kj9uh
@kaushalkumar-kj9uh - 14.10.2022 07:41

this prob is exactly same :1011. Capacity To Ship Packages Within D Days

Ответить
@akshitthakur5179
@akshitthakur5179 - 22.11.2022 08:39

binary search approach of this problem is similar to book allocation problem pattern, just in case someone is wondering about the intuition

Ответить
@yan-vn5oy
@yan-vn5oy - 27.11.2022 22:12

good explanations as aways

Ответить
@khanhquoc5352
@khanhquoc5352 - 28.11.2022 05:19

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.

Ответить
@italianpipes1849
@italianpipes1849 - 11.12.2022 05:31

Grazie mille!

Ответить
@joydeeprony89
@joydeeprony89 - 29.12.2022 15:00

why low = max(nums) ?

Ответить
@ygwg6145
@ygwg6145 - 14.01.2023 20:46

Maybe dividing cumsum curve into m even pieces can lead to more efficient solution.

Ответить
@gokulrajr
@gokulrajr - 21.01.2023 09:49

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...

Ответить
@Kidpunk98
@Kidpunk98 - 20.02.2023 01:53

Is it just me, or is 37 the wrong value for initial right? The total sum of numbers is 42.

Ответить
@shrimpo6416
@shrimpo6416 - 31.03.2023 19:51

hi everyone, I'm the dum dum that uses backtrack without cache 🤣 I'm so glad I learned the bin search solution!

Ответить
@lambar0
@lambar0 - 27.05.2023 00:04

Neat Code … simple and precise

Ответить
@AniketSingh-mt6zr
@AniketSingh-mt6zr - 11.07.2023 14:49

classy explanation

Ответить
@lazydoner6552
@lazydoner6552 - 22.07.2023 17:01

Is this question similar to book allocation problem of ggg?

Ответить
@soumyadeepdas1536
@soumyadeepdas1536 - 23.07.2023 23:34

It's basically the painters partition problem

Ответить
@shikshamishra5668
@shikshamishra5668 - 04.08.2023 14:35

can we do by keep target=sum(nums)//k and finding the split?

Ответить
@koeber99
@koeber99 - 08.08.2023 10:40

Why is the canSplit function ""return (subarry +1 <= m)"" and not """return ((subarry +1) == m) """? Don't we need to make sure all the splits can occur? For example, if the target is MAX_INTEGER (in case the sum of nums), then this will be true.

Ответить
@Chirayu19
@Chirayu19 - 22.08.2023 10:29

Can we build a prefix sum array and use it somehow to solve the problem greedily or binary search on the same array?

Ответить
@Shaaah_
@Shaaah_ - 24.09.2023 12:14

⬇Simple Code | TC: O( n*log( sum(nums) ) | SC: O( 1 )
int splitArray(vector<int>& nums, int m) {
int l=0,r=0,n=nums.size();
for(int i=0;i<n;++i) l=max(l,nums[i]), r+=nums[i];

int mid=0,ans=0;
while(l<=r){
mid=(l+r)/2;
int count=0,tempsum=0;
for(int i=0;i<n;++i){
if(tempsum+nums[i]<=mid) tempsum+=nums[i];
else count++,tempsum=nums[i];
}
count++;

if(count<=m) r=mid-1, ans=mid;
else l=mid+1;
}
return ans;
}

Ответить
@nitaikodkani
@nitaikodkani - 04.10.2023 01:54

was stuck on this one, thanks a lot for explaining it so well

Ответить
@mahendar7733
@mahendar7733 - 12.10.2023 16:27

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

Ответить
@i8you
@i8you - 07.01.2024 19:32

hey @NeetCode awesome explanation but i was wondering without return how the answer is returning .. don't we need to write return res ??

Ответить
@bouzie8000
@bouzie8000 - 25.02.2024 02:49

Lol this is truly a crackhead solution. God help us all

Ответить
@sarbajitde2547
@sarbajitde2547 - 14.03.2024 14:45

How is the code working without any return statement?

Ответить
@criticstar123
@criticstar123 - 09.04.2024 15:30

I used brute force method got the ans in the end but it is not optimal algorithm does it matter in interviews

Ответить
@kareni7572
@kareni7572 - 01.06.2024 15:40

Thanks for the dp recursive solution!

Ответить
@tenzin8773
@tenzin8773 - 27.06.2024 17:24

If i'm asked this question in the interview, I am beyond cooked

Ответить
@saurabhkumarsrivastava6034
@saurabhkumarsrivastava6034 - 13.08.2024 22:02

I think that it's the same as book allocation problem

Ответить
@vishalvanpariya1466
@vishalvanpariya1466 - 26.10.2024 09:22

subarray + 1 <= k

This line is because our above code does not add the last subarray, which is not counted in the subarray.

Ответить
@vijethkashyap151
@vijethkashyap151 - 23.11.2024 23:52

❤ Just gratitude man! Best channel ever!

Ответить
@shahzodshafizod
@shahzodshafizod - 16.12.2024 17:07

What the magic has happened without return res?

Ответить