forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoobing2.ts
More file actions
23 lines (22 loc) ยท 670 Bytes
/
soobing2.ts
File metadata and controls
23 lines (22 loc) ยท 670 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* ๋ฌธ์ ์ ํ
* - Array, DP
*
* ๋ฌธ์ ์ค๋ช
* - ๋ฐฐ์ด์์ "์ฐ์๋" ๋ถ๋ถ ๋ฐฐ์ด์ ํฉ ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ตฌํ๊ธฐ
*
* ์์ด๋์ด
* 1) Bottom-Up ๋ฐฉ์
* - dp์๋ ์์์๋ถํฐ ์ด์ด๋ถ์ธ ๊ฐ์ด ํฐ์ง, ํ์ฌ ๊ฐ์์ ๋ค์ ์์ํ๋๊ฒ ํด์ง ๋น๊ตํ์ฌ ํฐ ๊ฐ ์ ์ฅ (ํ์ฌ ๊ธฐ์ค)
* - maxSum์ ์ ์ฒด dp์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ์ ์ฅ
*/
function maxSubArray(nums: number[]): number {
const dp = new Array(nums.length);
dp[0] = nums[0];
let maxSum = nums[0];
for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
maxSum = Math.max(maxSum, dp[i]);
}
return maxSum;
}