Skip to content

Commit 5d953b9

Browse files
authored
[ PS ] : Maximum Subarray
1 parent 2dbaefc commit 5d953b9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

maximum-subarray/uraflower.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 주어진 배열에서 원소의 합이 가장 큰 부분 배열의 합을 반환하는 함수
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
const maxSubArray = function(nums) {
7+
let max = nums[0];
8+
let subSum = 0;
9+
10+
nums.forEach((num) => {
11+
subSum = Math.max(subSum + num, num);
12+
13+
if (max < subSum) {
14+
max = subSum;
15+
}
16+
});
17+
18+
return max;
19+
};
20+
21+
// 시간복잡도: O(n)
22+
// 공간복잡도: O(1)

0 commit comments

Comments
 (0)