Question:
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation: [4,-1,2,1] has the largest sum = 6.
Solution:
/*** @param {number[]} nums* @return {number}*/var maxSubArray = function(nums) {if(nums.length===0)return 0;let max = nums[0];for(let i=1; i<nums.length; i++){if(nums[i]<nums[i]+nums[i-1]){nums[i] = nums[i] + nums[i-1];}max = Math.max(max , nums[i]);}return max};
Runtime: 76 ms, faster than 48.44% of JavaScript online submissions for Maximum Subarray.
Memory Usage: 35.3 MB, less than 100.00% of JavaScript online submissions for Maximum Subarray.
