File tree 3 files changed +46
-0
lines changed
solution/0100-0199/0152.Maximum Product Subarray
3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,23 @@ class Solution {
76
76
}
77
77
```
78
78
79
+ ### ** TypeScript**
80
+
81
+ ``` ts
82
+ function maxProduct(nums : number []): number {
83
+ let n = nums .length ;
84
+ let preMax = nums [0 ], preMin = nums [0 ], ans = nums [0 ];
85
+ for (let i = 1 ; i < n ; ++ i ) {
86
+ let cur = nums [i ];
87
+ let x = preMax , y = preMin ;
88
+ preMax = Math .max (x * cur , y * cur , cur );
89
+ preMin = Math .min (x * cur , y * cur , cur );
90
+ ans = Math .max (preMax , ans );
91
+ }
92
+ return ans ;
93
+ };
94
+ ```
95
+
79
96
### ** C#**
80
97
81
98
``` cs
Original file line number Diff line number Diff line change @@ -72,6 +72,23 @@ class Solution {
72
72
}
73
73
```
74
74
75
+ ### ** TypeScript**
76
+
77
+ ``` ts
78
+ function maxProduct(nums : number []): number {
79
+ let n = nums .length ;
80
+ let preMax = nums [0 ], preMin = nums [0 ], ans = nums [0 ];
81
+ for (let i = 1 ; i < n ; ++ i ) {
82
+ let cur = nums [i ];
83
+ let x = preMax , y = preMin ;
84
+ preMax = Math .max (x * cur , y * cur , cur );
85
+ preMin = Math .min (x * cur , y * cur , cur );
86
+ ans = Math .max (preMax , ans );
87
+ }
88
+ return ans ;
89
+ };
90
+ ```
91
+
75
92
### ** C#**
76
93
77
94
``` cs
Original file line number Diff line number Diff line change
1
+ function maxProduct ( nums : number [ ] ) : number {
2
+ let n = nums . length ;
3
+ let preMax = nums [ 0 ] , preMin = nums [ 0 ] , ans = nums [ 0 ] ;
4
+ for ( let i = 1 ; i < n ; ++ i ) {
5
+ let cur = nums [ i ] ;
6
+ let x = preMax , y = preMin ;
7
+ preMax = Math . max ( x * cur , y * cur , cur ) ;
8
+ preMin = Math . min ( x * cur , y * cur , cur ) ;
9
+ ans = Math . max ( preMax , ans ) ;
10
+ }
11
+ return ans ;
12
+ } ;
You can’t perform that action at this time.
0 commit comments