Medium Problems

53. Maximum Subarray

What will be learned

  • Get introduced to dynamic programming and the Kadane's algorithm.

Solutionss

function maxSubArray(nums: number[]): number {
  let maxSub = nums[0]
  let curSum = 0
 
  for(let n of nums) {
      if (curSum < 0) {
          curSum = 0
      }
      curSum += n
      maxSub = Math.max(maxSub, curSum)
  }
 
  return maxSub
};

Video Solution

Last Update: 12:08 - 16 April 2024
Array
Divide and Conquer
Dynamic Programming

On this page