maximum sum subarray javascript
const maxSubArray = (nums) => { // initiate two variable, maxSum for total max, sum for current max let maxSum = -Infinity let currentSum = 0 // iterate through the nums, store sub-problems result for(let i = 0; i < nums.length; i++){ //cumulating answers to the top //compare currentSum add current number //with current number and store the maximum value currentSum = Math.max(nums[i], currentSum + nums[i]) //compare maxSum with currentSum and store the greater value maxSum = Math.max(currentSum, maxSum) } return maxSum }
Source: medium.com
largest sum contiguous subarray javascript
Using kadane's algorithm const maxSubArray = (nums) => { // initiate two variable, maxSum for total max, sum for current max let maxSum = -Infinity let currentSum = 0 // iterate through the nums, store sub-problems result for(let i = 0; i < nums.length; i++){ //cumulating answers to the top //compare currentSum add current number //with current number and store the maximum value currentSum = Math.max(nums[i], currentSum + nums[i]) //compare maxSum with currentSum and store the greater value maxSum = Math.max(currentSum, maxSum) } return maxSum }