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

Поделиться
HTML-код
  • Опубликовано: 29 сен 2024

Комментарии • 357

  • @NeetCode
    @NeetCode  4 года назад +46

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

  • @karanssh
    @karanssh 2 года назад +149

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

    • @reyou7
      @reyou7 5 месяцев назад

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

  • @Dhruvbala
    @Dhruvbala 2 месяца назад +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)

  • @SumTsuiTechie
    @SumTsuiTechie 2 года назад +80

    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 2 года назад +2

      This case will only happen if the curSum is negative anyways

    • @unanimous8510
      @unanimous8510 10 месяцев назад

      @@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 9 месяцев назад

      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 3 месяца назад

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

  • @DanielAkproh
    @DanielAkproh 2 года назад +290

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

    • @TeamDUSTOfficial
      @TeamDUSTOfficial 2 года назад +209

      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 2 года назад +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 2 года назад +79

      @@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 2 года назад +14

      @@TeamDUSTOfficial tanx man I got it here

    • @Avighna
      @Avighna 2 года назад +5

      Exactly what I was thinking!

  • @toolworks
    @toolworks 2 года назад +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 2 года назад +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 Год назад

      just add
      if(maxSum

  • @giorgizazadze1337
    @giorgizazadze1337 Месяц назад

    I would change to this because the code will fail if curSum is positive and the next element of the list is greater than curSum for example [1,2]. (It also works for all negative numbers)
    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    maxSub = nums[0]
    curSum = 0
    for n in nums:
    if curSum + n < n:
    curSum = 0
    curSum += n
    maxSub = max(curSum, maxSub)
    return maxSub

  • @CraftandDogs
    @CraftandDogs 2 года назад +2

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

    • @alexkarydis2270
      @alexkarydis2270 Год назад

      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.

  • @EranM
    @EranM 5 месяцев назад

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

  • @danielsun716
    @danielsun716 Год назад

    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)

  • @kachrooabhishek
    @kachrooabhishek 2 года назад +3

    but where is the condition to remove the negative values

  • @sachinshaji92
    @sachinshaji92 2 года назад +2

    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

    • @geraldo_silva
      @geraldo_silva 2 года назад

      This won't work... It will add up n to curSum twice

    • @sachinshaji92
      @sachinshaji92 2 года назад

      @@geraldo_silva it won't, test this entire code.
      cursum = 0
      maxsub=nums[0]
      for n in nums:
      if cursum < 0:
      cursum = n
      else:
      cursum += n
      maxsub=max(maxsub,cursum)
      print(maxsub)

  • @senseiAree
    @senseiAree Год назад

    I started doing LeetCode for the first time yesterday.... I have a six star in Problem Solving on Hackerrank and it just threw me over a cliff and smashed my organs on this Two Sum problem... My first problem on Leetcode was adding two numbers and then it jumped to this Two Sum

  • @sonofon
    @sonofon 28 дней назад

    Could someone explain why the two-pointer approach, where one pointer starts at the beginning and the other at the end of the array, and moving the pointer pointing to the smaller element, doesn't work?

  • @shaharrefaelshoshany9442
    @shaharrefaelshoshany9442 3 года назад +2

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

  • @EranM
    @EranM 5 месяцев назад

    holy.. I couldn't even come up with the cubic solution when tried to solve it.. Like HOW NAIVE Can ONE GET!! ??? :>>>

  • @Alex-rc4xd
    @Alex-rc4xd 2 года назад

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

  • @nisargbhatt4967
    @nisargbhatt4967 11 месяцев назад

    So this is just like the stock trading profit maximization problem

  • @varunkumarmedam7914
    @varunkumarmedam7914 2 года назад

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

  • @Jack-ss4re
    @Jack-ss4re 8 месяцев назад +1

    Everytime a see this as Easy question i die a little inside

    • @Rufioforever
      @Rufioforever 8 месяцев назад +2

      This was actually upgraded recently to a medium... so don't worry we can die a little less together. We're not crazy

  • @gompro
    @gompro 3 года назад +3

    beautiful explanation as always. Thank you

  • @raindrop2407
    @raindrop2407 2 года назад

    Wow. This is actually genius.

  • @JulianBoilen
    @JulianBoilen 2 года назад

    The question says to try 'divide and conquer'?

  • @YahyaNaeem-m7r
    @YahyaNaeem-m7r Месяц назад

    what if all elements were negative in the whole array ?

  • @yomamasofat413
    @yomamasofat413 4 месяца назад

    wtf I would never come up with this in a million years

  • @fwan0697
    @fwan0697 2 года назад

    Can you show the DP way of solving this?

  • @jasonl.7466
    @jasonl.7466 2 года назад

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

  • @AlexanderNekrasov-xu9oe
    @AlexanderNekrasov-xu9oe Год назад

    I don't think it works... We chop off anything off the left (!) if the sum so far is negative, but we fail to consider if chopping the same number off the right would be better; For example:
    (100 : -1M : 1 : 1 : 1 )
    This algorithm will see that at index 1 we have -999K and it will chop (0:1) off, and leave us with max sum = 3.

    • @YourHumbleFriend
      @YourHumbleFriend Год назад

      It should still work in that instance. We set the maxSub to 100, and as we continue along, that 100 is being compared against the 3 at the end.

  • @adnanakib4023
    @adnanakib4023 2 месяца назад

    If all the elements are negative, your last approach will fail.

  • @lkslokesh8842
    @lkslokesh8842 Год назад

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

  • @KaranSharma-ew7io
    @KaranSharma-ew7io 2 года назад +1

    what if all numbers are negative

  • @sharangkulkarni1759
    @sharangkulkarni1759 Год назад

    maja aa gya

  • @shivikamishra3690
    @shivikamishra3690 2 года назад

    What is the difference between "for n in nums " and "for n in range(len(nums))"? Can anyone help?

    • @NeetCode
      @NeetCode  2 года назад +1

      The second one will give you the index, the first will only give you the actual values.

    • @shivikamishra3690
      @shivikamishra3690 2 года назад

      @@NeetCode Thank you so much!

  • @fikrewold
    @fikrewold 2 года назад +37

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

    • @SkySentry7
      @SkySentry7 6 месяцев назад

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

    • @gautamanand6039
      @gautamanand6039 3 месяца назад

      what role?

  • @peteaustin5018
    @peteaustin5018 3 года назад +404

    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 3 года назад +20

      Its never too late

    • @peteaustin5018
      @peteaustin5018 3 года назад +114

      @@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 3 года назад +9

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

    • @celeschal
      @celeschal 2 года назад

      @@peteaustin5018 Yeee that's awesome, dude!

    • @djudsod959
      @djudsod959 2 года назад +1

      @@peteaustin5018 that's great.

  • @nehathakur5408
    @nehathakur5408 Год назад +6

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

    • @mohameddaffaallah6243
      @mohameddaffaallah6243 Год назад

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

    • @mohameddaffaallah6243
      @mohameddaffaallah6243 Год назад

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

  • @galinalazareva5829
    @galinalazareva5829 2 года назад +17

    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

    • @okaybossiamonyoutubeakkskd
      @okaybossiamonyoutubeakkskd 2 года назад

      ruclips.net/video/yBCzO0FpsVc/видео.html divide n conquer, this might help

    • @sergiofranklin8809
      @sergiofranklin8809 Год назад

      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 Год назад

      Did you get the solution for the follow up?

    • @MichaelButlerC
      @MichaelButlerC 11 месяцев назад

      I'm also interested as to what this means

    • @illu1na
      @illu1na 11 месяцев назад

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

  • @randomBoulevards
    @randomBoulevards 7 месяцев назад +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

  • @harsha4692
    @harsha4692 4 года назад +16

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

    • @kurokikazenootoko
      @kurokikazenootoko 4 года назад +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 3 года назад

      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 2 года назад +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

  • @SidharthSharma99
    @SidharthSharma99 8 месяцев назад +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;

  • @senthilkumarashokkumar8153
    @senthilkumarashokkumar8153 2 года назад +4

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

    • @amazing-graceolutomilayo5041
      @amazing-graceolutomilayo5041 2 года назад

      You want your problems solved with the lowest time complexity and in the video, he achieved n^1

  • @hereweare644
    @hereweare644 2 года назад +35

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

    • @sachinshaji92
      @sachinshaji92 2 года назад +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 2 года назад +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 2 года назад +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 2 года назад

      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 2 года назад +2

      It still works

  • @mnchester
    @mnchester 2 года назад +2

    This is now a Medium

  • @katharinah5105
    @katharinah5105 2 года назад +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 6 месяцев назад +3

    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!👍

  • @aaronw6485
    @aaronw6485 3 года назад +31

    Love the way you explain things super easy to follow along

  • @amir_coding
    @amir_coding Год назад +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 5 месяцев назад

      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

  • @gagemachado2597
    @gagemachado2597 3 года назад +18

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

    • @qqqqoeewqwer
      @qqqqoeewqwer 3 года назад +4

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

    • @dontdemotivate6575
      @dontdemotivate6575 3 года назад +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 3 года назад

      @@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 3 года назад +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 2 года назад +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

  • @ghostgutarist5234
    @ghostgutarist5234 4 месяца назад +1

    I dont think this solutioin work if all the numbers in the Array are negative !!! as u making it ZERO.

    • @Amanda-10702
      @Amanda-10702 2 месяца назад

      but if all nums are negative, then the max sum would just be the first value. because you will not get any larger sum if all the numbers are negative. so just simply return the first value.

  • @iamsumitgupta
    @iamsumitgupta 4 года назад +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 2 года назад

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

  • @TwoInaCanoe
    @TwoInaCanoe 2 года назад +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 2 года назад +5

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

  • @bhaskarbgd
    @bhaskarbgd 2 года назад +21

    Coming up with Kadane's algorithm intuitively is not an easy feat, especially in an interview. I think it would be better to show how to come up with divide and conquer/ recursive solutions and memoize them.

    • @SimonSaysFIFA
      @SimonSaysFIFA Год назад

      Exactly

    • @illu1na
      @illu1na 11 месяцев назад

      IMO divide and conquer for this algorithm is harder to implement than kadane's. I just did both methods on leetcode. Much easier to make mistake there.

  • @silencethy8287
    @silencethy8287 2 года назад +2

    I thought this was a simpler way to write it,
    def maxSubArray(self, nums: List[int]) -> int:
    maxVal = nums[0]
    for i in range(len(nums)-1):
    nums[i+1] = max(nums[i+1], nums[i] + nums[i+1])
    maxVal = max(nums[i+1], maxVal)
    return maxVal

  • @cankatadiloglu6387
    @cankatadiloglu6387 2 года назад +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 Год назад

      I agree

    • @HanSmellsGood
      @HanSmellsGood Год назад

      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 Год назад +1

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

    • @tarsco4235
      @tarsco4235 Год назад

      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.

  • @Tarekconqueso
    @Tarekconqueso 9 месяцев назад +2

    what if there are only negative numbers in the subarray ?

    • @Pigchun
      @Pigchun 7 месяцев назад

      thats what im thinking

    • @hwang1607
      @hwang1607 5 месяцев назад +1

      This still works since it will reset cursum to 0 at each index, for an array with only negative numbers the maximum subarray would only be one number, the one that is the largest

  • @NKATRUKUSUMANADH
    @NKATRUKUSUMANADH Год назад +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

  • @gplambi
    @gplambi 6 месяцев назад +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.

  • @DavidDLee
    @DavidDLee Год назад +1

    Perhaps explain why all negative input array will work too.

    • @randytruong6621
      @randytruong6621 Год назад

      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

  • @jakestring6027
    @jakestring6027 7 месяцев назад +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 ?

  • @mjmim
    @mjmim 2 года назад +5

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

  • @Enzoerb
    @Enzoerb 3 месяца назад

    Another possible problem is returning the range of the subarray instead of the sum, then we would have to keep track of more stuff

  • @m____s
    @m____s 4 месяца назад

    More intuitive solution:
    def maxSubArray(nums) :
    global_max = nums[0]
    curr_sum = nums[0]
    for value in nums[1:]:
    curr_sum = max(value, curr_sum+value)
    global_max = max(global_max, curr_sum)
    return global_max

  • @MainDoodler
    @MainDoodler 3 месяца назад

    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    best_combination_sum = -math.inf
    current_potentially_best_combination_sum = 0
    for num in nums:
    previous_winner_with_new_num = current_potentially_best_combination_sum + num
    current_potentially_best_combination_sum = max(previous_winner_with_new_num, num)
    best_combination_sum = max(current_potentially_best_combination_sum, best_combination_sum)
    return best_combination_sum

  • @ashiklj5100
    @ashiklj5100 3 месяца назад

    What if all the elements in the array are negative?
    The problem can be solved using Kadane's algorithm, check the java solution below:
    class Solution {
    public int maxSubArray(int[] nums) {
    int globalMax = nums[0];
    int currMax = nums[0];
    for (int i=1; i globalMax) {
    globalMax = currMax;
    }
    }
    return globalMax;
    }
    }

  • @cirog.9341
    @cirog.9341 Год назад +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?

    • @荔枝-c2y
      @荔枝-c2y Год назад

      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

  • @rebornreaper194
    @rebornreaper194 Год назад

    This problem is ridiculous. More math than coding challenge. And the ultimate efficiency is so arbitrary, b/c I've never seen any real world scenario asking anything like this _that also has a large enough set to warrant optimization_.

  • @sadsadpanda123
    @sadsadpanda123 3 месяца назад

    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.

  • @sohelsayyad5572
    @sohelsayyad5572 Месяц назад

    maxsub will return sum of maximum subarray, and not slice of list which constitutes maximum subarray .... right/?

  • @akashShinde_7030
    @akashShinde_7030 5 месяцев назад

    This solution is not accepting by Leetcode so i made few changes
    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    maxsum = float('-inf')
    CurrSum = 0
    for no in nums:
    CurrSum = max(no, CurrSum + no)
    maxsum = max(CurrSum,maxsum)
    return maxsum

  • @medharawat
    @medharawat Месяц назад +1

    paaji tusi great ho

  • @pavanakumarpk2525
    @pavanakumarpk2525 9 дней назад

    You should have kept maxSum instead of maxSub as it would create confusion

  • @karanpodar
    @karanpodar 6 месяцев назад

    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

  • @vamsikrishnagannamaneni912
    @vamsikrishnagannamaneni912 Год назад +1

    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.

  • @HR-pz7ts
    @HR-pz7ts Год назад +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.

  • @Dan-sy9uw
    @Dan-sy9uw 2 года назад +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 Год назад

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

    • @owenwu7995
      @owenwu7995 Год назад

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

    • @ashbywoulds
      @ashbywoulds Год назад

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

  • @haha-eg8fj
    @haha-eg8fj 2 года назад

    I just don't know how is your thought process from a two-pointer sliding window drawing to such a solution. It seems completely irrelevant.

  • @AbhinavKokrady
    @AbhinavKokrady 3 месяца назад

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

  • @mohajeramir
    @mohajeramir 2 года назад +1

    me crying and laughing at the same time

  • @jc3209
    @jc3209 5 месяцев назад

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

  • @Dygit
    @Dygit 4 месяца назад

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

  • @phongtranquoc7557
    @phongtranquoc7557 3 месяца назад

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

  • @parthsalat
    @parthsalat 4 месяца назад +1

    Please use dark mode

  • @frankl1
    @frankl1 2 года назад +2

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

  • @bleekr0439
    @bleekr0439 Год назад

    Given an array of just negative numbers wouldnt the highest be 0 instead of the biggest number out of all the neg numbers

  • @scherbakovmike
    @scherbakovmike Год назад

    This is a wrong solution, test on nums=[-2,-1], for example.

  • @baleygrsteysionfayf9818
    @baleygrsteysionfayf9818 Год назад

    The code is very efficient so efficient my monkey brain cant keep up with whats happening in the code lmao

  • @cplusplussizeddick1430
    @cplusplussizeddick1430 2 года назад +1

    Ok but the issue I see with this, or at least your explanation, is what if the array is [-5, -10] for instance. Now you get rid of the -5 since it's negative as you say, but the -10 is even more negative

    • @nghiavo6263
      @nghiavo6263 2 года назад

      in the end the loop, he compared the max and the left to avoid this case

  • @indraxios
    @indraxios Год назад +1

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

  • @jayantkhurana813
    @jayantkhurana813 8 месяцев назад

    save more time ->
    class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
    maxsum = nums[0]
    currsum = 0
    for n in nums:
    currsum += n
    if maxsum < currsum:
    maxsum = currsum
    if currsum < 0:
    currsum = 0
    return maxsum

  • @mahmoudsorour8073
    @mahmoudsorour8073 Год назад

    the only question i have
    how the fuck you got the same runtime in the last two submit 😂😂😂😂

  • @MichaelShingo
    @MichaelShingo Год назад

    crazy algorithm, note you can also just set the maxSub = -math.inf initially so it follows the pattern of other max value problems.

  • @jackchu1201
    @jackchu1201 Год назад +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

  • @shivakiranreddy4654
    @shivakiranreddy4654 2 года назад +1

    You are ignoring the negative sum, what if the max sum is a negative one

  • @МадиЮсупов-ю7ф
    @МадиЮсупов-ю7ф 4 года назад +5

    Thanks, great explanation and good visuals.

  • @tlee-t7b
    @tlee-t7b 2 месяца назад

    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.

  • @KP-jx1wy
    @KP-jx1wy 2 года назад +1

    Interviews are basically about testing how much time someone spent studying these optimal solutions. Who could come with this on their own if they have never seen the optimal solutions before

    • @GoonCity777
      @GoonCity777 2 года назад

      It sucks that nobody will get hired if they are not Kadane or if they didn’t go on leetcode or watch neetcode before

  • @unanimous8510
    @unanimous8510 10 месяцев назад

    To be honest with you, after I've watched your explanation I didn't understand why it was working. Then I watched another video about Kadane's algorithm and I understood that fine. Then I broke down both solutions - even though the values of `currentSum` and `maxSum` are equal for both algos, I still don't understand why your solution is working, which is shameful of me and ridiculous.

  • @ianokay
    @ianokay Год назад +1

    The "if (curSum < 0) curSum = 0" is what's super confusing about this problem to me and had me wonder "Wait, what about a list with all negative numbers?"

    • @ianokay
      @ianokay Год назад +1

      The multiple insights required to understand such a simple line:
      * This line essentially "restarts" the substring count from this number.
      Settings it to 0 is like having done no prior computations.
      * It will not make 0 the highest number, overriding highest negatives.
      * Negative maximums will still be found, but they will not include (be summed with) ANY other number in the list.
      This is because any other number, if positive, would by itself be a higher value.
      * Any other negative number, by itself, is the highest of all negative number combinations.
      * Even if the value is negative, and will not be compounded with any other number, it will still be added to our consideration as a solitary (non-summed/combined) max number via the max() call, so long as we never initialized our maxTotal to 0 (because that would take precedent as being higher).

    • @jbaltariu
      @jbaltariu Год назад +1

      the maximum sum in an array with negative numbers is the number with the maximum value

  • @sahilverma6160
    @sahilverma6160 Год назад

    A little simple approach
    a=[-2,1,-3,4,-1,2,1,-5,4]
    v=0
    for i in range(len(a)):
    for p in range(len(a[i:])):
    if v