DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Maximum Subarray: LeetCode

var maxSubArray = function(nums) {

  let max = nums[0];
  let maxEnding = nums[0];
  for (let i = 1; i < nums.length; i++) {
    maxEnding = Math.max(maxEnding + nums[i], nums[i]);
    max = Math.max(maxEnding, max);
  }
  return max;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)