Skip to content

Commit a835cb2

Browse files
feat: maximum-subarray 풀이
1 parent 928bba8 commit a835cb2

File tree

1 file changed

+18
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)