File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var maxProduct = function ( nums ) {
6+ // μ΅λ κ³±μ μ μ₯ν λ³μ
7+ let maxProduct = nums [ 0 ] ;
8+ // νμ¬ μμΉκΉμ§μ μ΅λ κ³±κ³Ό μ΅μ κ³±μ μ μ₯ν λ³μ
9+ let currentMax = nums [ 0 ] ;
10+ let currentMin = nums [ 0 ] ;
11+
12+ // λ°°μ΄μ λ λ²μ§Έ μμλΆν° μν
13+ for ( let i = 1 ; i < nums . length ; i ++ ) {
14+ const num = nums [ i ] ;
15+
16+ // μμλ₯Ό κ³±ν κ²½μ° μ΅λμ μ΅μκ° λ°λ μ μμΌλ―λ‘ λ―Έλ¦¬ μ μ₯
17+ const tempMax = currentMax ;
18+
19+ // νμ¬ μ«μμ κ³±νμ λμ μ΅λ/μ΅μ κ°μ κ³μ°
20+ currentMax = Math . max ( num , num * currentMax , num * currentMin ) ;
21+ currentMin = Math . min ( num , num * tempMax , num * currentMin ) ;
22+
23+ // μ 체 μ΅λ κ³±μ μ
λ°μ΄νΈ
24+ maxProduct = Math . max ( maxProduct , currentMax ) ;
25+ }
26+
27+ return maxProduct ;
28+ } ;
You canβt perform that action at this time.
0 commit comments