Skip to content

Commit 54506df

Browse files
committed
add: maximum-subarray
1 parent a148775 commit 54506df

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

maximum-subarray/HerrineKim.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 시간 복잡도 : O(n)
2+
// 공간 복잡도 : O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var maxSubArray = function(nums) {
9+
let currentSum = nums[0];
10+
let maxSum = nums[0];
11+
12+
for (let i = 1; i < nums.length; i++) {
13+
currentSum = Math.max(nums[i], currentSum + nums[i]);
14+
maxSum = Math.max(maxSum, currentSum);
15+
}
16+
17+
return maxSum;
18+
};

0 commit comments

Comments
 (0)