Maximum Subarray - Amazon Coding Interview Question - Leetcode 53 - Python

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    Twitter: / neetcode1
    Discord: / discord
    💡 CODING SOLUTIONS: • Coding Interview Solut...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    🌲 TREE PLAYLIST: • Invert Binary Tree - D...
    💡 GRAPH PLAYLIST: • Course Schedule - Grap...
    💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
    💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
    Solve it yourself: neetcode.io/problems/maximum-...
    0:00 - Cubic solution
    2:02 - Quadratic solution
    3:10 - Linear solution
    6:37 - Coding
    #Coding #Programming #CodingInterview
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 349

  • @NeetCode
    @NeetCode  Před 4 lety +45

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews

  • @karanssh
    @karanssh Před 2 lety +130

    Excellent explanation. For anyone curious, the algorithm in question in the O(N) solution is kadane's algorithm

    • @reyou7
      @reyou7 Před 3 měsíci

      Kadane!!!! If I get into FAANG, I will leave a rose to your tombstone Kadane!

  • @peteaustin5018
    @peteaustin5018 Před 3 lety +383

    Hey man, i'm a postgrad CS student who didn't do anywhere near enough programming during my spare time in my bachelors. Your algorithms are really helping me think differently, thank you.

    • @thiswasme5452
      @thiswasme5452 Před 2 lety +20

      Its never too late

    • @peteaustin5018
      @peteaustin5018 Před 2 lety +109

      @@thiswasme5452 If you were curious as to how this helped me, I am now working as a software engineer for a good organisation and I also aced my postgrad academic studies :)

    • @thiswasme5452
      @thiswasme5452 Před 2 lety +9

      @@peteaustin5018 Ohh great!! Nice to hear that 😊.

    • @celeschal
      @celeschal Před 2 lety

      @@peteaustin5018 Yeee that's awesome, dude!

    • @djudsod959
      @djudsod959 Před 2 lety +1

      @@peteaustin5018 that's great.

  • @FaithandLoveOverFear
    @FaithandLoveOverFear Před 2 lety +265

    But if all negative results are disregarded, doesn't that exclude the case where all integers of the given array are negative?

    • @TeamDUSTOfficial
      @TeamDUSTOfficial Před 2 lety +196

      If all elements are negative integers, the max subarray will just be the maximum integer, because adding any negative value to an integer will make it smaller. And we will get this max integer, because we will reset the subarray every iteration and check for the max value.

    • @somewheresomeone3959
      @somewheresomeone3959 Před 2 lety +35

      @@TeamDUSTOfficial Well but the current sum was set for zero, and it'll be the result after the max function so the maxSub will be zero instead of the first element

    • @TeamDUSTOfficial
      @TeamDUSTOfficial Před 2 lety +71

      @@somewheresomeone3959 You're adding the value of n at every iteration in the line `curSum += n` before the comparison so curSum won't be zero

    • @yeabseratesfayebekele5847
      @yeabseratesfayebekele5847 Před 2 lety +13

      @@TeamDUSTOfficial tanx man I got it here

    • @Avighna
      @Avighna Před rokem +5

      Exactly what I was thinking!

  • @hadi3k
    @hadi3k Před rokem +2

    great stuff brother, your explanations are so good by themselves, I dont even have to watch you code it up it just makes sense

  • @aaronw6485
    @aaronw6485 Před 2 lety +30

    Love the way you explain things super easy to follow along

  • @fadypy
    @fadypy Před 2 lety +1

    Of all the explanations on this algorithm, this video is the best. Thanks Man

  • @Dhruvbala
    @Dhruvbala Před 22 dny +2

    I find it easier to understand Kadane's algorithm as bottom up DP optimized for constant memory. The max subarray sum starting at index i is nums[i]+max(0,dp[i+1]). In other words, it's the maximum of choosing to keep just nums[i] or include the maximum of elements after it (which may be negative)

  • @fikrewold
    @fikrewold Před 2 lety +27

    Just did an interview with Amazon! This was the question.
    Unfortunately, I was thinking differently! Wish, I had watched this very well.

    • @SkySentry7
      @SkySentry7 Před 4 měsíci

      You ethiopian I reckon from your profile. Did you land a job at faang?

    • @gautamanand6039
      @gautamanand6039 Před měsícem

      what role?

  • @firstdbzmaker
    @firstdbzmaker Před 2 lety +3

    These video solutions are changing my life. Thanks Neet!

  • @SumTsuiTechie
    @SumTsuiTechie Před 2 lety +76

    I think the part for whether to keep the prefix is: if (n > n + curSum) curSum = n; else curSum = curSum + n.
    In human language means if me alone is better than all of us combine, you guys are no use for me.

    • @sahil_tayade
      @sahil_tayade Před 2 lety +2

      This case will only happen if the curSum is negative anyways

    • @unanimous8510
      @unanimous8510 Před 8 měsíci

      @@sahil_tayade that particular part I didn't understand in that solution... what if the previous curSum was even less than that and less than zero too...

    • @BaoziBandit
      @BaoziBandit Před 7 měsíci

      I agree with this, in the case explained by @NeetCode, if the array is filled with only negative values, then the max would be 0, even if 0 doesn't exist in the array.... So, the check provided by @SumTsuiTechie sounds like a good approach to resolving that.

    • @jaredgentry406
      @jaredgentry406 Před měsícem

      n > n + curSum => n - n > n - n + curSum => 0 > curSum => curSum < 0

  • @elizabethr5161
    @elizabethr5161 Před rokem +2

    One of the best and cleanest solutions, I have ever seen.

  • @srinadhp
    @srinadhp Před 2 lety +3

    I forgot to thank you again for a great explanation. You are my savior!

  • @Alex-rc4xd
    @Alex-rc4xd Před 2 lety

    Thank you so much for your videos! They've been exceptionally helpful.

  • @katharinah5105
    @katharinah5105 Před 2 lety +9

    Great video and explanation! Could you also explain how to solve the max subarray problem with divide and conquer? Thanks in advice!

  • @licokr
    @licokr Před 3 měsíci +1

    I've searched Kadane's Algorithm and solved this problem after understanding first. But I don't know how to explain this, so I've watched your video. I knew this I would learn from your video even after solving a problem without much problems. I learn from your videos how to think about a problem more than how to solve a problem. Negative values don't contribute anything to get the maximum number, that is why it intializes to zero to throw the previous max number which is a negative number. Great, thanks a lot!👍

  • @SidharthSharma99
    @SidharthSharma99 Před 5 měsíci +7

    Thanks for the series. Also Solution for the case where all numbers in array are negatives is returning the max number (which will be least negative number)
    int max = nums[0];
    int curMax = 0;
    int maxNumber = INT_MIN;
    for(int i=0;i < nums.size();i++)
    {
    curMax = curMax + nums.at(i);
    if( curMax < 0)
    {
    maxNumber = std::max(maxNumber, curMax);
    curMax = 0;
    }
    else
    max = std::max(curMax, max);
    }
    return (max < 0) ? maxNumber : max;

  • @user-yj2mr5we3k
    @user-yj2mr5we3k Před 4 lety +5

    Thanks, great explanation and good visuals.

  • @ruksharalam173
    @ruksharalam173 Před rokem +1

    This is such a simple and effective explanation. Thanks a lot, man :)

  • @mjmim
    @mjmim Před 2 lety +5

    Thank you Sir. I have made a silly mistake in this problem now it's clear.

  • @randomBoulevards
    @randomBoulevards Před 5 měsíci +2

    Even more concise and easy to understand solution possible:
    # initialise both variables to negative infinity:
    maxSub, curSum = float('-inf'), float('-inf')
    for n in nums:
    curSum = max(curSum + n, n)
    maxSub = max(maxSub, curSum)
    return maxSub

  • @oscarwork6579
    @oscarwork6579 Před 2 lety

    love ur tone and voice so much, good for listening the content into my brain

  • @aaronalexander8826
    @aaronalexander8826 Před 4 lety +2

    Thanks for the video, it was super helpful

  • @iamsumitgupta
    @iamsumitgupta Před 3 lety +9

    Hi, what device and software do you use for draw the figures by free hand and use it in your screen recording?

    • @pekarna
      @pekarna Před 2 lety

      I hear his mouse clicking, so likely just mouse. And there are many sw for over-the-screen drawing, like gInk.

  • @abdulnarimanov2256
    @abdulnarimanov2256 Před 4 lety +1

    Thanks for the video. Very happy to find this video

  • @Utkarshgg
    @Utkarshgg Před 2 lety

    this teaches us the real working of kaden's algo....awesome question!

  • @alishahbaz6062
    @alishahbaz6062 Před 2 lety +1

    I think you have to first add the total, and then check the condition if total is zero.

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 Před 3 lety +2

    amazing,
    could you please make this q with divide and conquer?

  • @yeseniaodalyss3029
    @yeseniaodalyss3029 Před 3 lety

    breaking up the video into different operation times >>>>>

  • @josemaria666
    @josemaria666 Před rokem

    In my opinion, this is de best video that I have found about this problem

  • @frankl1
    @frankl1 Před 2 lety +2

    Does this solution work if the array contains only negative integers?

  • @gompro
    @gompro Před 3 lety +3

    beautiful explanation as always. Thank you

  • @galinalazareva5829
    @galinalazareva5829 Před 2 lety +15

    Do you have any ideas about the Leetcode follow-up question for that problem? "Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle." Seems, they've added the follow-up recently

    • @loopzz5526
      @loopzz5526 Před rokem

      czcams.com/video/yBCzO0FpsVc/video.html divide n conquer, this might help

    • @sergiofranklin8809
      @sergiofranklin8809 Před rokem

      ChatGPT gave this solution for divide and conquer:
      def maxSubArray(nums):
      def divide_and_conquer(nums, left, right):
      if left == right:
      return nums[left]
      mid = (left + right) // 2
      # Find the maximum sum subarray in the left half
      left_max = float('-inf')
      curr_sum = 0
      for i in range(mid, left - 1, -1):
      curr_sum += nums[i]
      left_max = max(left_max, curr_sum)
      # Find the maximum sum subarray in the right half
      right_max = float('-inf')
      curr_sum = 0
      for i in range(mid + 1, right + 1):
      curr_sum += nums[i]
      right_max = max(right_max, curr_sum)
      # Find the maximum sum subarray crossing the middle element
      cross_max = left_max + right_max
      # Recursively find the maximum subarray sum in the left and right halves
      return max(cross_max, divide_and_conquer(nums, left, mid), divide_and_conquer(nums, mid + 1, right))
      # Call the divide and conquer function to find the maximum subarray sum
      return divide_and_conquer(nums, 0, len(nums) - 1)
      explanation: The idea behind this algorithm is to divide the input array into two halves, and recursively find the maximum subarray sum in the left and right halves. The maximum subarray sum can either lie entirely in the left half, entirely in the right half, or cross the middle element. The maximum subarray sum that crosses the middle element can be found by computing the maximum sum subarray in the left half that ends at the middle element, and the maximum sum subarray in the right half that starts from the middle element.
      The time complexity of this algorithm is O(n log n), where n is the size of the input array.

    • @shriharikulkarni3986
      @shriharikulkarni3986 Před 10 měsíci

      Did you get the solution for the follow up?

    • @MichaelButlerC
      @MichaelButlerC Před 9 měsíci

      I'm also interested as to what this means

    • @illu1na
      @illu1na Před 9 měsíci

      @@shriharikulkarni3986check the solution section in the leecode there is this one guy who showed 7 different solutions

  • @harsha4692
    @harsha4692 Před 3 lety +16

    what is the maximum sum is a negative number, the code would only output 0 then

    • @kurokikazenootoko
      @kurokikazenootoko Před 3 lety +11

      Not really. Assume the array only contains negative numbers. At start maxSub will equal to first element. Then in every iteration we do reset maxSub to zero, but we immediately sum it with next number (a negative too) making it equal to that number. So it will check first element against each next element, each time updating maxSub to the max of two. That's just classic "find max" one-pass algorithm.

    • @stevefox7418
      @stevefox7418 Před 3 lety

      In addition to what Сергей said, notice how we are storing the max element in maxSub and then returning that. So in the case of an array containing all negative numbers, it will return the maximum negative number.

    • @sachinshaji92
      @sachinshaji92 Před 2 lety +1

      For the code to work in any scenario(All Negative,All Positive,Mix) make this change:
      for n in nums:
      if curSum< 0:
      curSum= n

  • @HR-pz7ts
    @HR-pz7ts Před rokem +1

    Does it guarantees that it has positive numbers too? I saw this solution in the solutions of leetcode and I was confused as why it works.

  • @jasonl.7466
    @jasonl.7466 Před 2 lety

    what if the edge case of all negative numbers, is empty sub-array allowed ?

  • @NKATRUKUSUMANADH
    @NKATRUKUSUMANADH Před rokem +1

    what if i need to print the index values:
    will it take another loop and it make another o(N)
    or any other approach rather than loop?
    could anyone help me????
    Thanks in advance

  • @luansouzasilva31
    @luansouzasilva31 Před rokem

    Why does the runtime is so big when submitting to leetcode? Is this time related to a bunch of robustness tests right?

  • @parthsalat
    @parthsalat Před 2 měsíci +1

    Beautifully explained!

  • @bikashgurung6407
    @bikashgurung6407 Před 4 lety

    Thanks for the video bro!

  • @jakestring6027
    @jakestring6027 Před 5 měsíci +1

    What about if the test case was an array of all negative integers? Wouldn't we end up returning zero when the actual answer is the least negative of those given integers? Since curSum is initialized to 0 and at no point will our "max(maxSub, curSum)" code return anything other than 0 since it will always be higher than a negative number ?

  • @smarty7193
    @smarty7193 Před 2 lety

    very good explanation, Thanks

  • @Andrew_J123
    @Andrew_J123 Před 2 lety +1

    A solution that works the same but is a bit less clear is
    def MaxSubArray(nums):
    for i in range(1,len(nums)):
    if nums[i-1] > 0:
    nums[i] += nums[i-1]
    return max(nums)

  • @hereweare644
    @hereweare644 Před 2 lety +35

    Hi sir. What if the array is [-1,-2,-3,-4] like this?

    • @sachinshaji92
      @sachinshaji92 Před 2 lety +11

      For the code to work in any scenario(All Negative,All Positive,Mix) make this change:
      for n in nums:
      if curSum< 0:
      curSum= n

    • @milkyicy9482
      @milkyicy9482 Před 2 lety +1

      I guess you can add an if statement if the max number in the array is negative, just output the max number
      if max(nums)

    • @smarty7193
      @smarty7193 Před 2 lety +6

      it will work irrespective of input
      max=-1
      sum=0
      -----
      for loop
      -----
      sum=-1
      max =-1
      ------
      sumsum
      so max will remain -1
      ----
      and so on

    • @yosefco3
      @yosefco3 Před 2 lety

      class Solution:
      def maxSubArray(self, nums: List[int]) -> int:
      m = nums[0]
      s = nums[0]
      for n in nums[1:]:
      if n > s and s < 0:
      s = n
      else:
      s += n
      m = max(m, s)
      return m

    • @yonahgraphics
      @yonahgraphics Před 2 lety +2

      It still works

  • @gplambi
    @gplambi Před 4 měsíci +1

    Great explanation but i have one doubt .. There may be an array of all negative numbers and in such scenario , one subarray may sums up to -5 other subarray to -10 so -5 will be the answer. So curSum < 0 may not work in this case. Please correct me if i am wrong.

  • @mostafamabrouk8806
    @mostafamabrouk8806 Před 2 lety +6

    Thank you, I really understood it. but did we use dynamic programming in this solution?

    • @CostaKazistov
      @CostaKazistov Před 2 lety +13

      This way of solving this problem is known as Kadane's Algorithm.
      Kadane’s Algorithm is an *iterative dynamic programming* algorithm in which we search for a maximum sum contiguous subarray within a one-dimensional numeric array.

  • @its_neel_ok
    @its_neel_ok Před rokem

    i tried to code in c++ but failed :( what should be the approach/code for in c++

  • @kachrooabhishek
    @kachrooabhishek Před 2 lety +3

    but where is the condition to remove the negative values

  • @raindrop2407
    @raindrop2407 Před 2 lety

    Wow. This is actually genius.

  • @stratuspei9405
    @stratuspei9405 Před 2 měsíci

    This was the easiest explanation and solution of this problem. Played it at 1.5x and was able to understand

  • @nehathakur5408
    @nehathakur5408 Před rokem +5

    what happens if all the values are negative in the array ?

    • @mohameddaffaallah6243
      @mohameddaffaallah6243 Před 10 měsíci

      the curSum will always reset to each negative number in the array and the maxSub will keep the highest number

    • @mohameddaffaallah6243
      @mohameddaffaallah6243 Před 10 měsíci

      add this at the end of the forloop for a clearer understanding
      print(f"curSum = {curSum} MaxSub = {maxSub}")

  • @EranM
    @EranM Před 3 měsíci

    neetcode please return making videos with this keyboard.. I really get relaxed from this. Even if I solved this, I see the typing part! :>

  • @tlee-t7b
    @tlee-t7b Před 21 hodinou

    Here's my additional thoughts on the third solution, Kadane's algo. It works by basically directly locating the start of the subarray while updating the end of the subarray (which is what the single loop is doing).
    Compared to the quadratic solution, Kadane's algo skips the outer loop, because whenever the curSum < 0, resetting it to 0 is the same effort of chopping off the front portion/placing the start of the subarray to the correct stating index, aka. getting rid of all combinations with a starting index being anyone in the front portion. And if this starting index proceeds to the end within this current round in the loop without curSum ever becoming negative, it automatically already got rid of the rest of the combinations with a starting index after this current starting index, since all those combinations afterwards can only lead to a smaller sum because they will be a shorter subarray missing this positive front portion to be added that could make it bigger. That's why Kadane's algo can be linear because it basically gets rid of the outer loop in the quadratic solution.

  • @omi_naik
    @omi_naik Před 2 lety

    Great Explanation :)

  • @TwoInaCanoe
    @TwoInaCanoe Před 2 lety +6

    One edge case is missing. What if all numbers are negative [-1, -3, -4]. We need to return -1. But this algorithm will return 0.

    • @AGuilmon
      @AGuilmon Před 2 lety +4

      It won't, the code keeps track of the max sum seen. Inside the loop, it initializes the current sum to 0 if it's negative, then proceeds to add the current number (negative in this case) to the current sum. Then, if the current sum is bigger than the max sum seen, it will set max sum to the current sum (in this case a negative). It will keep track of the smallest negative seen (the smaller a negative the bigger it is compared to other negatives).

  • @cirog.9341
    @cirog.9341 Před 10 měsíci +1

    Hi! Thank you for the video, it provided me with a few insights. One question, so this algorithm won't work if the array contains only negative numbers?

    • @user-mm8fg4gv5s
      @user-mm8fg4gv5s Před 10 měsíci

      It doesn't matter, since when you come to nums[p] if the preSum < 0, you set it back to 0, and now current sum is just nums[p], then you use max() to recored the new maxSum, even if all numbers is negative, if nums[p] is the biggest one, you will find and record it in this step

  • @Dygit
    @Dygit Před 2 měsíci

    Doesn't resetting curSum to 0 assume that there are values in our array that are >= 0? What if all our values are negative?

  • @varswe
    @varswe Před rokem +1

    i though there is no intuition behind this algo, i just need to memorize it, but you changed my mind

  • @manojramesh4598
    @manojramesh4598 Před 7 měsíci

    but if we put curSum

  • @artmispotter3571
    @artmispotter3571 Před 2 lety

    thanks man, big help!

  • @riteshkedar3445
    @riteshkedar3445 Před rokem

    If I want to return both subarray and the maximum sum how can i do that

  • @user-bz7gt4zi7r
    @user-bz7gt4zi7r Před rokem

    Can't you just set the curSum to the current number if maxSub < curSum?

  • @AbhinavKokrady
    @AbhinavKokrady Před měsícem

    This was very helpful, please make a Divide and conquer solution for this too.

  • @priyanshagarwal8490
    @priyanshagarwal8490 Před 5 měsíci

    Can you make a video for 163. Missing Ranges.. Your videos have been succinct and very helpful.

  • @phongtranquoc7557
    @phongtranquoc7557 Před měsícem

    I dont understand why if current sum is negative then it doesnt contribute anything can you explain more on this issue?

  • @mohammedsohail5599
    @mohammedsohail5599 Před 6 měsíci

    Does sorting work on this problem? And Keep adding the numbers till the value decreases?

  • @jc3209
    @jc3209 Před 3 měsíci

    This solution wouldn't work for the case where all numbers in the array are negative, would it?

  • @canberkpower
    @canberkpower Před 2 lety +1

    Smart solution! (This should be a medium question.)

  • @user-ev7zv5te4t
    @user-ev7zv5te4t Před rokem

    What if the whole array is made of negative numbers? Would this still work?

  • @gagemachado2597
    @gagemachado2597 Před 3 lety +17

    Hey, quick question about the linear time solution, would this code blow up given an array of all negative values?

    • @qqqqoeewqwer
      @qqqqoeewqwer Před 3 lety +4

      still work, the max element value of the array will be the answer

    • @dontdemotivate6575
      @dontdemotivate6575 Před 3 lety +5

      @@qqqqoeewqwer I don't think that it will work
      as my input is [-1]
      or [-2,-2,-3,-1,-4,-5]
      because by this code answer will 0

    • @ThePunlee
      @ThePunlee Před 3 lety

      @@dontdemotivate6575 have you tried running the code yet? Answer is not going to be zero, since there is the maxSum variable that stores the answer to your example: [-2,-2,-3,-1,-4,-5].

    • @ic2800
      @ic2800 Před 2 lety +1

      it won't. best way to learn this is to run this code and print the part that you are unclear. given all negative number the max() function will return the largest negative number of the array

    • @sachinshaji92
      @sachinshaji92 Před 2 lety +1

      For the code to work in any scenario(All Negative,All Positive,Mix) make this change:
      for n in nums:
      if curSum< 0:
      curSum= n

  • @toolworks
    @toolworks Před 2 lety +12

    I think leetcode added new test cases since this solution was uploaded. My initial attempt was like this but the input array [-1] broke it. My solution when "resetting" the subTotal to zero was to instead check to see whether the new subTotal is less than the value itself (previous sum + currentVal < currentVal). I initialised the max_sum as negative infinity to allow for the fact that subarrays can have sums less than zero, but it did feel a bit hacky using float() to do that.
    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    max_sum = float('-inf')
    current_sum = 0
    for num in nums:
    current_sum += num
    if num > current_sum:
    current_sum = num
    if current_sum > max_sum:
    max_sum = current_sum
    return max_sum

    • @issasecretbuddy
      @issasecretbuddy Před 2 lety +1

      the optimized solution works for this, since we're always initializing the max sum to the first number in the array and adding the current number to the currentSum before deciding whether it should replace the previous max. So, an array of [-1] would start with max of -1 and current of 0, then hit the loop. In the loop it would first add 0 + (-1) and then compare -1 (curSum) to -1 (maxSum).
      IMO the minimum sum should always be 0 since an empty subarray should add to 0, but hey, i guess the problem calls for a subarray of at least one item.

    • @jayberry6557
      @jayberry6557 Před rokem

      just add
      if(maxSum

  • @vamsikrishnagannamaneni912

    Initially, I confidently declared, "I don't even need a pen and paper to solve it in O(n) time." But as I struggled, scratching my head in confusion, I eventually found myself here, seeking assistance and realizing that maybe a pen and paper wouldn't have been such a bad idea after all.

  • @lkslokesh8842
    @lkslokesh8842 Před 11 měsíci

    hey what if the whole array is negative.. will it work??

  • @Aditya-yk9lt
    @Aditya-yk9lt Před 2 lety

    hey i have a doubt, when I have coded in python the line i have added was
    if cursum

    • @sanjasme
      @sanjasme Před 2 lety

      This will fail when there's an array containing only negative numbers.
      In this scenario, the result should be the max number in the array, but your solution will return 0

  • @HazardKnight13
    @HazardKnight13 Před 2 měsíci

    Thanks for the explanation :D What if we wanted to create a subarray of the maximum? so in your example it would output [4, -1, 2, 1]

  • @sadsadpanda123
    @sadsadpanda123 Před měsícem

    Lovely solution but as others have pointed out, this fails the leetcode 53 test suite because that has things like [-2, -1] as test patterns whereas by resetting to 0 constantly it won't work for those.

  • @amitthakur5684
    @amitthakur5684 Před 2 lety

    If all the elements are negative then how we get max subArray

  • @Dan-sy9uw
    @Dan-sy9uw Před rokem +2

    Hi, i was just thinking about it, but if all numbers are negative, wont your answer be returning 0, which is not inside the array?

    • @mowww20
      @mowww20 Před rokem

      No, it will return the biggest numbers in the array.

    • @owenwu7995
      @owenwu7995 Před rokem

      @@mowww20 Then why are we setting currentsum to 0 if it's less than 0?

    • @ashbywoulds
      @ashbywoulds Před 10 měsíci

      @owenwu7995 Setting currentSum to 0 is to reset where the sub array starts. maxSub is always going to return the bigger of the two sub arrays (current biggest total vs newest total)

  • @senthilkumarashokkumar8153

    why brute force is n^3 ?. It looks with n^2 , we can solve it. Can you explain why ?

  • @cankatadiloglu6387
    @cankatadiloglu6387 Před rokem +2

    I think starting current with 0 is wrong. Apparently this is not in the test cases but the array could be [-3, -2, -1] so max sub array would be -1. I say we should compare the current subarray with the current number instead of 0, like so;
    def maxSubArray(self, nums: List[int]) -> int:
    maximum = current = nums[0]
    for i in range(1, len(nums)):
    current = max(current+nums[i], nums[i])
    maximum = max(maximum, current)
    return maximum

    • @vinhnghiang1273
      @vinhnghiang1273 Před rokem

      I agree

    • @HanSmellsGood
      @HanSmellsGood Před rokem

      the answer would still be -1 because after current is set to 0, it still performs += num which for e.g is -1, hence when perform max(max, current), -1 would still be the result

  • @daming-xing
    @daming-xing Před 10 měsíci +1

    I don't not fully understand the if (cursum < 0) part. why 0?

    • @tarsco4235
      @tarsco4235 Před 10 měsíci

      you'll get a lower answer than the highest max you can get if you don't reset it to 0, and you can't set it higher than zero because your total will be higher than the max sub array. you can get away with "forcing" a current sum of zero because it helps you reach your maximum.

  • @danielsun716
    @danielsun716 Před rokem

    DP solution FYI:
    def maxSubArray(self, nums: List[int]) -> int:
    dp = [0] * len(nums)
    dp[len(nums) - 1] = nums[len(nums) - 1]
    for i in range(len(nums) - 2, -1, -1):
    dp[i] = max(nums[i], nums[i] + dp[i + 1])
    return max(dp)

  • @omarahmedabouelela7989
    @omarahmedabouelela7989 Před 2 lety +1

    What if the maximum is a negative number ?

  • @963gal
    @963gal Před rokem

    What happens that the all the array is negative?

  • @bibeksharma600
    @bibeksharma600 Před rokem

    Another Sliding window appraoch with two pointers:
    Python Soln:
    maxS = nums[0]
    summ = 0
    i,j = 0,0
    while j

    • @wangfred
      @wangfred Před rokem

      Thanks! this is much easier to understand!

    • @wangfred
      @wangfred Před rokem

      wait, we don't need the i pointer.

  • @eswartc2877
    @eswartc2877 Před rokem

    will this work if all the elements are negative?

  • @amir_coding
    @amir_coding Před 10 měsíci +1

    why are negative values ignored? what if all the elements are negatives?
    that case you have to find minimum negative number right? somebody answer this.

    • @rafaelbnn
      @rafaelbnn Před 2 měsíci

      You're right, if all the elements are negative the answer will be the greatest negative. Lines 7 and 8 clear the curSum everytime so that we don't add up more than one negative number, and then we 'reinitialize' curSum with the current value, and check if it's bigger than the one we have.
      I agree that this is a pretty neat way to do it. If I had come up with the solution by myself I would never had thought of something so elegant

  • @JulianBoilen
    @JulianBoilen Před 2 lety

    The question says to try 'divide and conquer'?

  • @Aditya-ne4lk
    @Aditya-ne4lk Před 2 lety

    high quality content

  • @varunkumarmedam7914
    @varunkumarmedam7914 Před rokem

    Why are you making curSum =0, because max sum can be negative also right?

  • @monojit104
    @monojit104 Před 2 lety

    Could you please make a video for the leetcode 1335?

  • @shriharikulkarni3986
    @shriharikulkarni3986 Před 10 měsíci

    They've asked for the divide and conquer approach, can u give that solution ?

  • @karanpodar
    @karanpodar Před 3 měsíci

    for n in nums:
    if curSum < 0:
    curSum = 0
    curSum += n

    maxSum = max(maxSum, curSum)
    curSum = maxSum
    return maxSum
    I think this logic is better as it will handle all negative values as well

  • @TheRedTeam
    @TheRedTeam Před 2 lety

    What if they are all negative numbers. I do not think the solution would handle that.

  • @DavidDLee
    @DavidDLee Před rokem +1

    Perhaps explain why all negative input array will work too.

    • @randytruong6621
      @randytruong6621 Před rokem

      Because the maxSub holds the current max and if you keep adding negatives together its turns to a bigger negative so you would return the highest negative number in the array

  • @chengyiliu2277
    @chengyiliu2277 Před 2 lety

    is it possible the max sum is negative?

  • @rudranshsharma2074
    @rudranshsharma2074 Před 2 lety

    Thanks a lot sir :)

  • @CraftandDogs
    @CraftandDogs Před 2 lety +2

    I don't think we should remove the negative as we might have an array of negative numbers

    • @alexkarydis2270
      @alexkarydis2270 Před 10 měsíci

      I thought the same and struggled with it that's why I came here. Reality is that from an array of negative numbers you should pick the largest of those numbers as your largest sum. The solution sets the current sum to zero if it's negative but then adds the current number (which is negative) to the current sum (which is zero) and compares it to the largest sum. So the largest number out of those negative numbers gets selected eventually.

  • @jackchu1201
    @jackchu1201 Před rokem +8

    Updated code from the NeetCode website shown below. (The code in the video may not have the right order in terms of resetting the total vs updating the result)
    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    res = nums[0]
    total = 0
    for n in nums:
    total += n
    res = max(res, total)
    if total < 0:
    total = 0
    return res

  • @abhimalyachowdhury7835
    @abhimalyachowdhury7835 Před 2 lety +1

    what if all the array elements are negative? won't maxSub return 0?

    • @erikm9768
      @erikm9768 Před 2 lety

      no, it will just go through each the elements in curSum and keep the biggest in bestSum