Skip to content

Commit 7b30acd

Browse files
committed
maximum subarray solution
1 parent 0ae64c6 commit 7b30acd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

maximum-subarray/hyer0705.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxSubArray(nums: number[]): number {
2+
const numsLen = nums.length;
3+
let currentSum = nums[0];
4+
let maxSum = nums[0];
5+
6+
for (let i = 1; i < numsLen; i++) {
7+
currentSum = Math.max(currentSum + nums[i], nums[i]);
8+
maxSum = Math.max(maxSum, currentSum);
9+
}
10+
11+
return maxSum;
12+
}

0 commit comments

Comments
 (0)