Skip to content

Commit f198072

Browse files
committed
maximum subarray solution
1 parent 3864ab5 commit f198072

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

maximum-subarray/krokerdile.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxSubArray = function(nums) {
6+
let maxSum = nums[0];
7+
let currentSum = nums[0];
8+
9+
for (let i = 1; i < nums.length; i++) {
10+
// 지금 원소 자체가 더 클 수도 있음 (부분합 리셋)
11+
currentSum = Math.max(nums[i], currentSum + nums[i]);
12+
maxSum = Math.max(maxSum, currentSum);
13+
}
14+
15+
return maxSum;
16+
};

0 commit comments

Comments
 (0)