forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclara-shin.js
More file actions
41 lines (34 loc) Β· 1.18 KB
/
clara-shin.js
File metadata and controls
41 lines (34 loc) Β· 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* μ μ λ°°μ΄μμ μ°μλ λΆλΆλ°°μ΄(subarray)μ κ³±μ΄ μ΅λκ° λλ κ°μ μ°Ύλ ν¨μ
*
* μμλΌλ¦¬ κ³±νλ©΄ μμκ° λ¨
* 0μ΄ λμ€λ©΄ κ³±μ 0μ΄ λ¨ => λΆλΆλ°°μ΄μ μλ‘ μμν΄μΌ ν¨
* μ΅λκ°κ³Ό μ΅μκ°μ λμμ μΆμ ν΄μΌ ν¨: μμμ κ²½μ° μ΅λκ°μ΄ μ΅μκ°μ΄ λ μ μμ
*
* μ κ·Ό λ°©λ²: λμ νλ‘κ·Έλλ°
* μκ°λ³΅μ‘λ: O(n), 곡κ°λ³΅μ‘λ: O(1)
*/
/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function (nums) {
if (nums.length === 0) return 0;
// νμ¬ μμΉμμ λλλ λΆλΆλ°°μ΄μ μ΅λκ³±κ³Ό μ΅μκ³±
let maxHere = nums[0];
let minHere = nums[0];
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
const num = nums[i];
// νμ¬ μ«μκ° μμλΌλ©΄ maxμ minμ΄ λ°λ μ μμ
if (num < 0) {
[maxHere, minHere] = [minHere, maxHere];
}
// νμ¬ μ«μλΆν° μλ‘ μμνκ±°λ, κΈ°μ‘΄ κ³±μ νμ¬ μ«μλ₯Ό κ³±ν¨
maxHere = Math.max(num, maxHere * num);
minHere = Math.min(num, minHere * num);
// μ 체 μ΅λκ° μ
λ°μ΄νΈ
result = Math.max(result, maxHere);
}
return result;
};