File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ // μκ°λ³΅μ‘λ: O(n)
4+ public int maxProduct (int [] nums ) {
5+
6+ if (nums == null || nums .length == 0 ) {
7+ return 0 ;
8+ }
9+
10+ // μ΅λ κ³± μΌμ΄μ€
11+ int maxProduct = nums [0 ];
12+ // μ΅μ κ³± μΌμ΄μ€
13+ int minProduct = nums [0 ];
14+ // μ΅λ κ°
15+ int max = nums [0 ];
16+
17+ // DPλ‘ νμ΄νμμ§λ§ μμ * μμμ λ°λ‘ μΌμ΄μ€ λ°μνμ¬ ν΄λΉ νμ΄λ‘ μμ
18+ // Test Case : [-2, 3, -4] => dpλ‘ νμ΄ μ 3μ΄ λ°νλλ λ¬Έμ μμμ
19+
20+ for (int i = 1 ; i < nums .length ; i ++) {
21+
22+ int current = nums [i ];
23+
24+ // μμμΌ κ²½μ° : μ΅μ κ³±κ³Ό μ΅λ κ³± μμΉ λ°κΏ
25+ if (current < 0 ) {
26+ int tempMax = maxProduct ;
27+ maxProduct = Math .max (current , minProduct * current );
28+ minProduct = Math .min (current , tempMax * current );
29+ } else {
30+ maxProduct = Math .max (current , maxProduct * current );
31+ minProduct = Math .min (current , minProduct * current );
32+ }
33+
34+ max = Math .max (max , maxProduct );
35+
36+ }
37+
38+ return max ;
39+
40+ }
41+ }
42+
You canβt perform that action at this time.
0 commit comments