File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ // TC: O(N)
2+ // SC: O(1)
3+
4+ /**
5+ * @param {number[] } nums
6+ * @return {number }
7+ */
8+ var maxProduct = function ( nums ) {
9+ let maximumProduct = Number . MIN_SAFE_INTEGER ;
10+ let subProduct = 1 ;
11+ // 1. μ’μμ μ°λ‘ λμ κ³±μ μ μ₯νκΈ° μν΄ μν
12+ for ( let index = 0 ; index < nums . length ; index ++ ) {
13+ // 2. 0μ λ§λλ©΄ λμ κ³±μ κ³±νμ§ μκ³ 1λ‘ μ΄κΈ°ν
14+ if ( nums [ index ] === 0 ) {
15+ maximumProduct = Math . max ( maximumProduct , 0 ) ;
16+ subProduct = 1 ;
17+ continue ;
18+ }
19+ // 3. λ§€λ² λμ κ³±μ κ°±μ
20+ subProduct *= nums [ index ] ;
21+ maximumProduct = Math . max ( maximumProduct , subProduct ) ;
22+ }
23+
24+ subProduct = 1 ;
25+ // 4. μ°μμ μ’λ‘ λμ κ³±μ μ μ₯νκΈ° μν΄ μν
26+ for ( let index = nums . length - 1 ; index >= 0 ; index -- ) {
27+ // 5. 0μ λ§λλ©΄ λμ κ³±μ κ³±νμ§ μκ³ 1λ‘ μ΄κΈ°ν
28+ if ( nums [ index ] === 0 ) {
29+ maximumProduct = Math . max ( maximumProduct , 0 ) ;
30+ subProduct = 1 ;
31+ continue ;
32+ }
33+ // 6. λ§€λ² λμ κ³±μ κ°±μ
34+ subProduct *= nums [ index ] ;
35+ maximumProduct = Math . max ( maximumProduct , subProduct ) ;
36+ }
37+
38+ return maximumProduct ;
39+ } ;
You canβt perform that action at this time.
0 commit comments