File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
solution/0100-0199/0123.Best Time to Buy and Sell Stock III Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -195,6 +195,24 @@ public class Solution {
195
195
}
196
196
```
197
197
198
+ ### ** TypeScript**
199
+
200
+ ``` ts
201
+ function maxProfit(prices : number []): number {
202
+ let f1 = - prices [0 ],
203
+ f2 = 0 ,
204
+ f3 = - prices [0 ],
205
+ f4 = 0 ;
206
+ for (let i = 1 ; i < prices .length ; ++ i ) {
207
+ f1 = Math .max (f1 , - prices [i ]);
208
+ f2 = Math .max (f2 , f1 + prices [i ]);
209
+ f3 = Math .max (f3 , f2 - prices [i ]);
210
+ f4 = Math .max (f4 , f3 + prices [i ]);
211
+ }
212
+ return f4 ;
213
+ }
214
+ ```
215
+
198
216
### ** ...**
199
217
200
218
```
Original file line number Diff line number Diff line change @@ -161,6 +161,24 @@ public class Solution {
161
161
}
162
162
```
163
163
164
+ ### ** TypeScript**
165
+
166
+ ``` ts
167
+ function maxProfit(prices : number []): number {
168
+ let f1 = - prices [0 ],
169
+ f2 = 0 ,
170
+ f3 = - prices [0 ],
171
+ f4 = 0 ;
172
+ for (let i = 1 ; i < prices .length ; ++ i ) {
173
+ f1 = Math .max (f1 , - prices [i ]);
174
+ f2 = Math .max (f2 , f1 + prices [i ]);
175
+ f3 = Math .max (f3 , f2 - prices [i ]);
176
+ f4 = Math .max (f4 , f3 + prices [i ]);
177
+ }
178
+ return f4 ;
179
+ }
180
+ ```
181
+
164
182
### ** ...**
165
183
166
184
```
Original file line number Diff line number Diff line change
1
+ function maxProfit ( prices : number [ ] ) : number {
2
+ let f1 = - prices [ 0 ] ,
3
+ f2 = 0 ,
4
+ f3 = - prices [ 0 ] ,
5
+ f4 = 0 ;
6
+ for ( let i = 1 ; i < prices . length ; ++ i ) {
7
+ f1 = Math . max ( f1 , - prices [ i ] ) ;
8
+ f2 = Math . max ( f2 , f1 + prices [ i ] ) ;
9
+ f3 = Math . max ( f3 , f2 - prices [ i ] ) ;
10
+ f4 = Math . max ( f4 , f3 + prices [ i ] ) ;
11
+ }
12
+ return f4 ;
13
+ }
You can’t perform that action at this time.
0 commit comments