Skip to content

Commit cc752e5

Browse files
committed
solve: 최대 부분 배열 문제 풀이 추가 - O(n) 시간복잡도로 구현
1 parent 1aa2775 commit cc752e5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

maximum-subarray/reach0908.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 시간복잡도: O(n)
3+
* 시간: 4ms
4+
* @param {number[]} nums
5+
* @return {number}
6+
*/
7+
var maxSubArray = function (nums) {
8+
let maxResult = nums[0];
9+
let curResult = nums[0];
10+
11+
for (let i = 1; i < nums.length; i += 1) {
12+
const num = nums[i];
13+
// 지금까지 누적합 vs 현재 날짜
14+
result = Math.max(curResult + num, num);
15+
maxResult = Math.max(maxResult, curResult);
16+
}
17+
18+
return maxResult;
19+
};

0 commit comments

Comments
 (0)