Skip to content

Commit 15ce53b

Browse files
committed
maximum subarray solution
1 parent 3c1b105 commit 15ce53b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
10+
// ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ˆœํšŒ
11+
for (let i = 1; i < nums.length; i++) {
12+
// ์ด์ „๊นŒ์ง€์˜ ํ•ฉ์— ํ˜„์žฌ ์›์†Œ๋ฅผ ๋”ํ• ์ง€, ์•„๋‹ˆ๋ฉด ํ˜„์žฌ ์›์†Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์‹œ์ž‘ํ• ์ง€ ๊ฒฐ์ •
13+
currentSum = Math.max(nums[i], currentSum + nums[i]);
14+
// ์ „์ฒด ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
15+
maxSum = Math.max(maxSum, currentSum);
16+
}
17+
18+
return maxSum;
19+
};

0 commit comments

Comments
ย (0)