Skip to content

Commit d4a3e3d

Browse files
committed
256th Commit
1 parent 542cb4c commit d4a3e3d

File tree

4 files changed

+183
-6
lines changed

4 files changed

+183
-6
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,17 @@ Ace Coding Interview with 75 Qs
253253
[198]: ./src/page-2/198.%20House%20Robber/rob.ts
254254
[790]: ./src/page-8/790.%20Domino%20and%20Tromino%20Tiling/numTilings.ts
255255

256-
| DP - Multidimensional | | |
257-
| --------------------------------------------------------- | ---------------- | ------ |
258-
| 62. Unique Paths | [Solution][62] | Medium |
259-
| 1143. Longest Common Subsequence | [Solution][1143] | Medium |
260-
| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium |
261-
| 72. Edit Distance | Solution | Medium |
256+
| DP - Multidimensional | | | |
257+
| --------------------------------------------------------- | ---------------- | ------ | ----------------------- |
258+
| 62. Unique Paths | [Solution][62] | Medium | 詳解 |
259+
| 1143. Longest Common Subsequence | [Solution][1143] | Medium | 詳解 |
260+
| 714. Best Time to Buy and Sell Stock with Transaction Fee | [Solution][714] | Medium | [詳解][714-explanation] |
261+
| 72. Edit Distance | Solution | Medium | 詳解 |
262262

263263
[62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts
264264
[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts
265+
[714]: ./src/page-7/714.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/maxProfit.ts
266+
[714-explanation]: ./src/page-7/714.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README.md
265267

266268
| Bit Manipulation | | |
267269
| --------------------------------------------- | ---------------- | ------ |
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 714. 買賣股票的最佳時機含手續費 (Best Time to Buy and Sell Stock with Transaction Fee)
2+
3+
給定一個整數陣列 `prices`,其中 `prices[i]` 表示第 `i` 天的股票價格;整數 `fee` 代表了交易股票的手續費。
4+
5+
找出你能獲得的最大利潤。你可以完成任意數量的交易,但你需要為每筆交易支付手續費。
6+
7+
注意:
8+
9+
- 你不得同時進行多項交易 (即,您必須在再次購買之前出售股票)。
10+
- 每次買賣股票只收取一次手續費。
11+
12+
## 解題
13+
14+
使用動態規劃 (Dynamic Programming)
15+
16+
`prices = [1,3,2,8,4,9], fee = 2`
17+
18+
- cash:不持有股票時的最大利潤。
19+
- hold:持有股票時的最大利潤。
20+
21+
i = 0, price = 1:
22+
23+
- cash = 0
24+
- hold = -1,買入
25+
26+
i = 1, price = 3:
27+
28+
- cash = max(0, −1 + 3 − 2) = 0
29+
- hold = max(−1, 0 − 3) = −1
30+
31+
i = 1, price = 2:
32+
33+
- cash = max(0, −1 + 2 − 2) = 0
34+
- hold = max(−1, 0 − 2) = −1
35+
36+
i = 1, price = 8:
37+
38+
- cash = max(0, −1 + 8 − 2) = 5,賣出
39+
- hold = max(−1, 0 − 8) = −1
40+
41+
i = 1, price = 4:
42+
43+
- cash = max(5, -1 + 4 - 2) = 5
44+
- hold = max(−1, 5 − 4) = 1,買入
45+
46+
i = 1, price = 9:
47+
48+
- cash = max(5, 1 + 9 - 2) = 8,賣出
49+
- hold = max(1, 5 - 9) = 1
50+
51+
```ts
52+
export const maxProfit: MaxProfit = (prices, fee) => {
53+
const n = prices.length;
54+
const dp: number[][] = Array(n).fill([0, 0]);
55+
56+
// 初始值
57+
dp[0][0] = 0; // 第一天不持有股票,利潤為 0
58+
dp[0][1] = -prices[0]; // 第一天持有股票,利潤為 -prices[0]
59+
60+
// 狀態轉移
61+
for (let i = 1; i < n; i++) {
62+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee); // 不持有股票
63+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); // 持有股票
64+
}
65+
66+
// 最後一天不持有股票的最大利潤
67+
return dp[n - 1][0];
68+
};
69+
```
70+
71+
```ts
72+
export const maxProfit: MaxProfit = (prices, fee) => {
73+
const n = prices.length;
74+
const cash = Array(n).fill(0);
75+
const hold = Array(n).fill(0);
76+
77+
cash[0] = 0;
78+
hold[0] = -prices[0];
79+
80+
for (let i = 1; i < n; i++) {
81+
cash[i] = Math.max(cash[i - 1], hold[i - 1] + prices[i] - fee);
82+
hold[i] = Math.max(hold[i - 1], cash[i - 1] - prices[i]);
83+
}
84+
85+
return cash[n - 1];
86+
};
87+
```
88+
89+
```ts
90+
export const maxProfit: MaxProfit = (prices, fee) => {
91+
let cash = 0;
92+
let hold = -prices[0];
93+
94+
for (let i = 1; i < prices.length; i++) {
95+
cash = Math.max(cash, hold + prices[i] - fee);
96+
hold = Math.max(hold, cash - prices[i]);
97+
}
98+
99+
return cash;
100+
};
101+
```
102+
103+
## 解題 2
104+
105+
使用貪婪 (Greedy)
106+
107+
```ts
108+
export const maxProfit2: MaxProfit = (prices, fee) => {
109+
let profit = 0;
110+
let prevPrice = prices[0];
111+
112+
for (let i = 0; i < prices.length; i++) {
113+
if (prices[i] > prevPrice + fee) {
114+
profit += prices[i] - prevPrice - fee;
115+
prevPrice = prices[i] - fee;
116+
}
117+
118+
if (prices[i] < prevPrice) {
119+
prevPrice = prices[i];
120+
}
121+
}
122+
123+
return profit;
124+
};
125+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { maxProfit, maxProfit2 } from './maxProfit';
2+
3+
describe('714. Best Time to Buy and Sell Stock with Transaction Fee', () => {
4+
test('maxProfit', () => {
5+
expect(maxProfit([1, 3, 2, 8, 4, 9], 2)).toBe(8);
6+
expect(maxProfit([1, 3, 7, 5, 10, 3], 3)).toBe(6);
7+
});
8+
9+
test('maxProfit2', () => {
10+
expect(maxProfit2([1, 3, 2, 8, 4, 9], 2)).toBe(8);
11+
expect(maxProfit2([1, 3, 7, 5, 10, 3], 3)).toBe(6);
12+
});
13+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
type MaxProfit = (prices: number[], fee: number) => number;
2+
3+
/**
4+
* Accepted
5+
*/
6+
export const maxProfit: MaxProfit = (prices, fee) => {
7+
let cash = 0;
8+
let hold = -prices[0];
9+
10+
for (let i = 1; i < prices.length; i++) {
11+
cash = Math.max(cash, hold + prices[i] - fee);
12+
hold = Math.max(hold, cash - prices[i]);
13+
}
14+
15+
return cash;
16+
};
17+
18+
/**
19+
* Accepted
20+
*/
21+
export const maxProfit2: MaxProfit = (prices, fee) => {
22+
let profit = 0;
23+
let prevPrice = prices[0];
24+
25+
for (let i = 0; i < prices.length; i++) {
26+
if (prices[i] > prevPrice + fee) {
27+
profit += prices[i] - prevPrice - fee;
28+
prevPrice = prices[i] - fee;
29+
}
30+
31+
if (prices[i] < prevPrice) {
32+
prevPrice = prices[i];
33+
}
34+
}
35+
36+
return profit;
37+
};

0 commit comments

Comments
 (0)