File tree 1 file changed +35
-0
lines changed 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * 쑰건
3
+ * κ°μ₯ ν° λ°°μ΄ κ³±μ μ°Ύμμ return
4
+ * 32-bit integer
5
+ * -10<=num[i]<=10
6
+
7
+ * μμ΄λμ΄
8
+ * μ΄μ μ΅λκ°, μ΅μκ°μ ꡬν΄λλ€
9
+ * μ΅λκ³±μ΄ λμ¬ μ μλ κ²½μ°
10
+ * 1) μ΅μκ° κ³±νκ²(-*-)
11
+ * 2) μ΅λκ° κ³±νκ²(+*+)
12
+ * 3) μκΈ°μμ μΈ κ²½μ°(+)
13
+ * λ°°μ΄μ λλ©΄μ μΈ κ°μ§ μ€ μ΅λμ΅μκ°μ κ°±μ ν΄λκ°λ€
14
+ *
15
+ */
16
+ function maxProduct ( nums : number [ ] ) : number {
17
+ let max = nums [ 0 ] ;
18
+ let min = nums [ 0 ] ;
19
+ let result = nums [ 0 ] ;
20
+
21
+ for ( let i = 1 ; i < nums . length ; i ++ ) {
22
+ const candidates = [ nums [ i ] * max , nums [ i ] , nums [ i ] * min ] ;
23
+ let currMax = Math . max ( ...candidates ) ;
24
+ let currMin = Math . min ( ...candidates ) ;
25
+
26
+ max = currMax ;
27
+ min = currMin ;
28
+
29
+ result = Math . max ( currMax , result ) ;
30
+ }
31
+ return result ;
32
+ }
33
+
34
+ // TC: O(n)
35
+ // SC: O(1)
You canβt perform that action at this time.
0 commit comments