Skip to content

Instantly share code, notes, and snippets.

@alfasin
Created March 6, 2024 23:33
Show Gist options
  • Save alfasin/ac8b3c64b0d44183dd0463811263391d to your computer and use it in GitHub Desktop.
Save alfasin/ac8b3c64b0d44183dd0463811263391d to your computer and use it in GitHub Desktop.
Kadane's Algorithm
function maxSubArray(nums: number[]): number {
let max = nums[0];
let curSum = nums[0];
for (let i = 1; i < nums.length; i++) {
if (curSum < 0) curSum = nums[i];
else curSum += nums[i];
if (max < curSum) max = curSum;
}
return max;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment