Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8735e6b

Browse files
feat: add ts solution to lc problem: No.0123 (doocs#1314)
1 parent fef0d6f commit 8735e6b

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎solution/0100-0199/0123.Best Time to Buy and Sell Stock III/README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ public class Solution {
195195
}
196196
```
197197

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+
198216
### **...**
199217

200218
```

‎solution/0100-0199/0123.Best Time to Buy and Sell Stock III/README_EN.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ public class Solution {
161161
}
162162
```
163163

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+
164182
### **...**
165183

166184
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /