Split Array Largest Sum - Leetcode 410 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 95

  • @NeetCode
    @NeetCode  2 ปีที่แล้ว +5

    Python - github.com/neetcode-gh/leetcode/blob/main/410-Split-Array-Largest-Sum.py

  • @devdev19
    @devdev19 2 ปีที่แล้ว +27

    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 ปีที่แล้ว +3

      Indeed. I swear Im not gonna waste 10xtime on lc official solutions anymore, neetcode is the saviour!!

  • @niteshmanem1997
    @niteshmanem1997 2 ปีที่แล้ว +46

    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

  • @poptart007-b2r
    @poptart007-b2r 2 ปีที่แล้ว +27

    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 ปีที่แล้ว +8

    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.

  • @ketangupta1910
    @ketangupta1910 2 ปีที่แล้ว +41

    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 7 หลายเดือนก่อน

      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

  • @李順安-p3x
    @李順安-p3x 2 ปีที่แล้ว +13

    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?

  • @akshitthakur5179
    @akshitthakur5179 2 ปีที่แล้ว +6

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

  • @nishantingle1438
    @nishantingle1438 2 ปีที่แล้ว +4

    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 ปีที่แล้ว +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.

  • @zaidshaikh2536
    @zaidshaikh2536 2 ปีที่แล้ว +3

    Glad you made this video. This problem is indeed Unique in Itself.

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +2

      True, it's definitely not obvious

  • @vishalvanpariya1466
    @vishalvanpariya1466 หลายเดือนก่อน +1

    subarray + 1

  • @vdyb745
    @vdyb745 2 ปีที่แล้ว +1

    Mindbender !!! Would have never realized it is a binary search problem !!! Simply Brill !!!!! 👏👏👏🙇‍♂

  • @vijethkashyap151
    @vijethkashyap151 23 วันที่ผ่านมา

    ❤ 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

  • @JijaChad-os8mo
    @JijaChad-os8mo 2 ปีที่แล้ว +2

    Thank you so much bro for your explanations. But with your help, I am gaining some confidence in solving hard problems.

  • @nikhilsingh2184
    @nikhilsingh2184 2 ปีที่แล้ว +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

  • @nitaikodkani
    @nitaikodkani ปีที่แล้ว

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

  • @HardeepSingh-pi2hr
    @HardeepSingh-pi2hr 2 ปีที่แล้ว +3

    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 ปีที่แล้ว +6

      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 🙌

  • @SweetDarkViolet
    @SweetDarkViolet 2 ปีที่แล้ว +10

    Ohhh here it comes. Last daily challenge of March.

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +12

      Yeah, this coming sunday I will introduce something cool for the channel - you heard it here first :)

    • @darshanputtaswamy3199
      @darshanputtaswamy3199 2 ปีที่แล้ว

      What's that ?

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

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

  • @josephchen1344
    @josephchen1344 2 ปีที่แล้ว +1

    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 ปีที่แล้ว +2

      May be solving after sorting would do?

  • @soumikkarmakar7433
    @soumikkarmakar7433 2 ปีที่แล้ว +2

    Similar to "Allocate minimum number of pages" problem.

  • @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

  • @varunsen2802
    @varunsen2802 2 ปีที่แล้ว +2

    Thank you for this solution.

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +2

      No problem!

  • @sushantrocks
    @sushantrocks 2 ปีที่แล้ว

    similar to fair workload - there we can get the relation to bin search

  • @kareni7572
    @kareni7572 6 หลายเดือนก่อน

    Thanks for the dp recursive solution!

  • @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?

  • @albertchen5501
    @albertchen5501 2 ปีที่แล้ว +1

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

  • @criticstar123
    @criticstar123 8 หลายเดือนก่อน +1

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

  • @theearthish
    @theearthish 2 ปีที่แล้ว +5

    Was waiting for this lol 100% crackhead intuition needed. Amazing explanation as always 🙌

  • @Kidpunk98
    @Kidpunk98 ปีที่แล้ว +3

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

  • @neelmehta9092
    @neelmehta9092 2 ปีที่แล้ว +6

    The person who made this problem is evil

  • @shikshamishra5668
    @shikshamishra5668 ปีที่แล้ว

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

  • @i8you
    @i8you 11 หลายเดือนก่อน

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

  • @shrimpo6416
    @shrimpo6416 ปีที่แล้ว

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

  • @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

  • @lambar0
    @lambar0 ปีที่แล้ว

    Neat Code … simple and precise

  • @hkanything
    @hkanything 2 ปีที่แล้ว +3

    Where is the 37 comes from? 7+2+5+10+18 =42

    • @yunshuwang5078
      @yunshuwang5078 2 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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)

  • @sipwhitemocha
    @sipwhitemocha 2 ปีที่แล้ว

    Could someone please help me how R = sum(nums) is initially 37??? 8:55

  • @uditsharma5688
    @uditsharma5688 2 ปีที่แล้ว +1

    When the backtracking solution passed , was it faster than the binary search?

    • @NeetCode
      @NeetCode  2 ปีที่แล้ว +5

      Not even close, binary search is much more efficient

  • @DavidDLee
    @DavidDLee 6 หลายเดือนก่อน

    1. Missing return: code will not run
    2. 16:06 missed the mark on explaining the logic back in 10:03

  • @nishantingle1438
    @nishantingle1438 2 ปีที่แล้ว

    The Optimal solution is a pretty standard variation of Binary Search.
    Check Allocate Pages from GeeksForGeeks/Aditya Verma's TH-cam video or LC 1011.

  • @jugalparulekar661
    @jugalparulekar661 2 ปีที่แล้ว

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

  • @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

  • @lazydoner6552
    @lazydoner6552 ปีที่แล้ว

    Is this question similar to book allocation problem of ggg?

  • @AbdulAziz-fp3hz
    @AbdulAziz-fp3hz 2 ปีที่แล้ว

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

  • @rongrongmiao3018
    @rongrongmiao3018 2 ปีที่แล้ว

    Oh well I was just looking at this problem today

  • @AniketSingh-mt6zr
    @AniketSingh-mt6zr ปีที่แล้ว

    classy explanation

  • @alps747
    @alps747 2 ปีที่แล้ว

    I couldn't understand the explanation starting at 10:25 (th-cam.com/video/YUF3_eBdzsk/w-d-xo.html). 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()

  • @soumyadeepdas1536
    @soumyadeepdas1536 ปีที่แล้ว

    It's basically the painters partition problem

  • @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?

  • @yan-vn5oy
    @yan-vn5oy 2 ปีที่แล้ว

    good explanations as aways

  • @italianpipes1849
    @italianpipes1849 2 ปีที่แล้ว

    Grazie mille!

  • @superheroherohero
    @superheroherohero 2 ปีที่แล้ว

    Thank you!

  • @sarbajitde2547
    @sarbajitde2547 9 หลายเดือนก่อน

    How is the code working without any return statement?

  • @Zifox20
    @Zifox20 2 ปีที่แล้ว

    Easy Problem: How many videos do I need before you become my favourite youtuber?

  • @tenzin8773
    @tenzin8773 5 หลายเดือนก่อน

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

  • @shahzodshafizod
    @shahzodshafizod วันที่ผ่านมา

    What the magic has happened without return res?

  • @anybody413
    @anybody413 2 ปีที่แล้ว

    Thanks!

  • @saurabhkumarsrivastava6034
    @saurabhkumarsrivastava6034 4 หลายเดือนก่อน

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

  • @snex-techprogrammer5110
    @snex-techprogrammer5110 2 ปีที่แล้ว

    'return res' at the end

  • @joydeeprony89
    @joydeeprony89 ปีที่แล้ว

    why low = max(nums) ?

    • @yashwants18
      @yashwants18 ปีที่แล้ว

      Let say if m = len(nums) then what would be the answer? I guess max(nums)

  • @anthonyuccello1458
    @anthonyuccello1458 2 ปีที่แล้ว +2

    Solution is O(crackhead)

  • @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

  • @bchen1403
    @bchen1403 2 ปีที่แล้ว

    this is madness

  • @infiniteloop5449
    @infiniteloop5449 ปีที่แล้ว

    Crack Head Problems would be an interesting TH-cam List.

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

  • @bouzie8000
    @bouzie8000 9 หลายเดือนก่อน

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

  • @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?

  • @kaushalkumar-kj9uh
    @kaushalkumar-kj9uh 2 ปีที่แล้ว

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