Skip to content

Instantly share code, notes, and snippets.

@svaza
Created January 28, 2022 03:54
Show Gist options
  • Save svaza/021e77c009adcd386dde16ff0b9b72ef to your computer and use it in GitHub Desktop.
Save svaza/021e77c009adcd386dde16ff0b9b72ef to your computer and use it in GitHub Desktop.
maximum subarray
/**
* https://leetcode.com/problems/maximum-subarray/
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let maxSum = Number.MIN_SAFE_INTEGER;
let runningSum = 0;
for(let i = 0; i < nums.length; i++) {
runningSum = runningSum + nums[i];
maxSum = Math.max(maxSum, runningSum);
if(runningSum < 0)
runningSum = 0;
}
return maxSum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment