Skip to content

Commit 57cd633

Browse files
committed
solve(w03): 53. Maximum Subarray
1 parent d748504 commit 57cd633

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

maximum-subarray/jeongwoo903.js

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

0 commit comments

Comments
 (0)