Created
March 6, 2024 23:33
-
-
Save alfasin/ac8b3c64b0d44183dd0463811263391d to your computer and use it in GitHub Desktop.
Kadane's Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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