Skip to content

Commit add7376

Browse files
committed
solution Maximum Subarray (#275)
- #275
1 parent 8bc27b0 commit add7376

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function maxSubArray(nums: number[]): number {
2+
// ๋ฐฐ์—ด์ด ๋น„์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฒดํฌ (์ œ์•ฝ์กฐ๊ฑด์— ์˜ํ•ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์ง€๋งŒ, ๊ฒฌ๊ณ ํ•œ ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด)
3+
if (nums.length === 0) return 0;
4+
5+
// ํ˜„์žฌ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ํ•ฉ๊ณผ ์ „์ฒด ์ตœ๋Œ€ ๋ถ€๋ถ„ ๋ฐฐ์—ด ํ•ฉ ์ดˆ๊ธฐํ™”
6+
let currentSum = nums[0];
7+
let maxSum = nums[0];
8+
9+
// ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ์ˆœํšŒ
10+
for (let i = 1; i < nums.length; i++) {
11+
// ํ˜„์žฌ ์œ„์น˜์—์„œ์˜ ์ตœ๋Œ€ ๋ถ€๋ถ„ ๋ฐฐ์—ด ํ•ฉ ๊ณ„์‚ฐ
12+
// "์ด์ „๊นŒ์ง€์˜ ํ•ฉ + ํ˜„์žฌ ์š”์†Œ" vs "ํ˜„์žฌ ์š”์†Œ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์‹œ์ž‘"
13+
currentSum = Math.max(nums[i], currentSum + nums[i]);
14+
15+
// ์ „์ฒด ์ตœ๋Œ€๊ฐ’ ์—…๋ฐ์ดํŠธ
16+
maxSum = Math.max(maxSum, currentSum);
17+
}
18+
19+
return maxSum;
20+
}

0 commit comments

Comments
ย (0)