Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
添加(0714.买卖股票的最佳时机含手续费动态规划.md):增加typescript版本
  • Loading branch information
xiaofei-2020 committed May 13, 2022
commit ca2711164dfd39b51f1e6d96207673ffba29d3f1
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ const maxProfit = (prices,fee) => {
}
```

TypeScript:

```typescript
function maxProfit(prices: number[], fee: number): number {
/**
dp[i][0]:持有股票
dp[i][1]: 不持有
*/
const length: number = prices.length;
if (length === 0) return 0;
const dp: number[][] = new Array(length).fill(0).map(_ => []);
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (let i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
}
return dp[length - 1][1];
};
```




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