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 b91d3c2

Browse files
Merge pull request youngyangyang04#1327 from xiaofei-2020/dp39
添加(0714.买卖股票的最佳时机含手续费动态规划.md):增加typescript版本
2 parents 71a9111 + ca27111 commit b91d3c2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/0714.买卖股票的最佳时机含手续费(动态规划).md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ const maxProfit = (prices,fee) => {
200200
}
201201
```
202202

203+
TypeScript:
204+
205+
```typescript
206+
function maxProfit(prices: number[], fee: number): number {
207+
/**
208+
dp[i][0]:持有股票
209+
dp[i][1]: 不持有
210+
*/
211+
const length: number = prices.length;
212+
if (length === 0) return 0;
213+
const dp: number[][] = new Array(length).fill(0).map(_ => []);
214+
dp[0][0] = -prices[0];
215+
dp[0][1] = 0;
216+
for (let i = 1; i < length; i++) {
217+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
218+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
219+
}
220+
return dp[length - 1][1];
221+
};
222+
```
223+
224+
225+
203226

204227
-----------------------
205228
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

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