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;
}