Skip to content

Commit 96b184f

Browse files
Jeehay28Jeehay28
authored andcommitted
Add maximum-subarray solution in TypeScript
1 parent 92929b1 commit 96b184f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

maximum-subarray/Jeehay28.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Approach 1:
2+
// Time Complexity: O(n)
3+
// Space Complexity: O(1)
4+
5+
function maxSubArray(nums: number[]): number {
6+
let currentSum = nums[0];
7+
let maxSum = nums[0];
8+
9+
for (let i = 1; i < nums.length; i++) {
10+
currentSum = Math.max(nums[i], currentSum + nums[i]);
11+
maxSum = Math.max(currentSum, maxSum);
12+
}
13+
14+
return maxSum;
15+
}
16+

0 commit comments

Comments
 (0)