forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjiji-hoon96.ts
More file actions
20 lines (16 loc) ยท 742 Bytes
/
jiji-hoon96.ts
File metadata and controls
20 lines (16 loc) ยท 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function maxSubArray(nums: number[]): number {
// ๋ฐฐ์ด์ด ๋น์ด ์๋ ๊ฒฝ์ฐ ์ฒดํฌ (์ ์ฝ์กฐ๊ฑด์ ์ํด ๋ฐ์ํ์ง ์์ง๋ง, ๊ฒฌ๊ณ ํ ์ฝ๋๋ฅผ ์ํด)
if (nums.length === 0) return 0;
// ํ์ฌ ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ๊ณผ ์ ์ฒด ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ์ด๊ธฐํ
let currentSum = nums[0];
let maxSum = nums[0];
// ๋ ๋ฒ์งธ ์์๋ถํฐ ์ํ
for (let i = 1; i < nums.length; i++) {
// ํ์ฌ ์์น์์์ ์ต๋ ๋ถ๋ถ ๋ฐฐ์ด ํฉ ๊ณ์ฐ
// "์ด์ ๊น์ง์ ํฉ + ํ์ฌ ์์" vs "ํ์ฌ ์์๋ถํฐ ์๋ก ์์"
currentSum = Math.max(nums[i], currentSum + nums[i]);
// ์ ์ฒด ์ต๋๊ฐ ์
๋ฐ์ดํธ
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}