Skip to content

Commit

Permalink
256th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Dec 24, 2024
1 parent 542cb4c commit d4a3e3d
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ Ace Coding Interview with 75 Qs
[198]: ./src/page-2/198.%20House%20Robber/rob.ts
[790]: ./src/page-8/790.%20Domino%20and%20Tromino%20Tiling/numTilings.ts

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

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

| Bit Manipulation | | |
| --------------------------------------------- | ---------------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# 714. 買賣股票的最佳時機含手續費 (Best Time to Buy and Sell Stock with Transaction Fee)

給定一個整數陣列 `prices`,其中 `prices[i]` 表示第 `i` 天的股票價格;整數 `fee` 代表了交易股票的手續費。

找出你能獲得的最大利潤。你可以完成任意數量的交易,但你需要為每筆交易支付手續費。

注意:

- 你不得同時進行多項交易 (即,您必須在再次購買之前出售股票)。
- 每次買賣股票只收取一次手續費。

## 解題

使用動態規劃 (Dynamic Programming)

`prices = [1,3,2,8,4,9], fee = 2`

- cash:不持有股票時的最大利潤。
- hold:持有股票時的最大利潤。

i = 0, price = 1:

- cash = 0
- hold = -1,買入

i = 1, price = 3:

- cash = max(0, −1 + 3 − 2) = 0
- hold = max(−1, 0 − 3) = −1

i = 1, price = 2:

- cash = max(0, −1 + 2 − 2) = 0
- hold = max(−1, 0 − 2) = −1

i = 1, price = 8:

- cash = max(0, −1 + 8 − 2) = 5,賣出
- hold = max(−1, 0 − 8) = −1

i = 1, price = 4:

- cash = max(5, -1 + 4 - 2) = 5
- hold = max(−1, 5 − 4) = 1,買入

i = 1, price = 9:

- cash = max(5, 1 + 9 - 2) = 8,賣出
- hold = max(1, 5 - 9) = 1

```ts
export const maxProfit: MaxProfit = (prices, fee) => {
const n = prices.length;
const dp: number[][] = Array(n).fill([0, 0]);

// 初始值
dp[0][0] = 0; // 第一天不持有股票,利潤為 0
dp[0][1] = -prices[0]; // 第一天持有股票,利潤為 -prices[0]

// 狀態轉移
for (let i = 1; i < n; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee); // 不持有股票
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); // 持有股票
}

// 最後一天不持有股票的最大利潤
return dp[n - 1][0];
};
```

```ts
export const maxProfit: MaxProfit = (prices, fee) => {
const n = prices.length;
const cash = Array(n).fill(0);
const hold = Array(n).fill(0);

cash[0] = 0;
hold[0] = -prices[0];

for (let i = 1; i < n; i++) {
cash[i] = Math.max(cash[i - 1], hold[i - 1] + prices[i] - fee);
hold[i] = Math.max(hold[i - 1], cash[i - 1] - prices[i]);
}

return cash[n - 1];
};
```

```ts
export const maxProfit: MaxProfit = (prices, fee) => {
let cash = 0;
let hold = -prices[0];

for (let i = 1; i < prices.length; i++) {
cash = Math.max(cash, hold + prices[i] - fee);
hold = Math.max(hold, cash - prices[i]);
}

return cash;
};
```

## 解題 2

使用貪婪 (Greedy)

```ts
export const maxProfit2: MaxProfit = (prices, fee) => {
let profit = 0;
let prevPrice = prices[0];

for (let i = 0; i < prices.length; i++) {
if (prices[i] > prevPrice + fee) {
profit += prices[i] - prevPrice - fee;
prevPrice = prices[i] - fee;
}

if (prices[i] < prevPrice) {
prevPrice = prices[i];
}
}

return profit;
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { maxProfit, maxProfit2 } from './maxProfit';

describe('714. Best Time to Buy and Sell Stock with Transaction Fee', () => {
test('maxProfit', () => {
expect(maxProfit([1, 3, 2, 8, 4, 9], 2)).toBe(8);
expect(maxProfit([1, 3, 7, 5, 10, 3], 3)).toBe(6);
});

test('maxProfit2', () => {
expect(maxProfit2([1, 3, 2, 8, 4, 9], 2)).toBe(8);
expect(maxProfit2([1, 3, 7, 5, 10, 3], 3)).toBe(6);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type MaxProfit = (prices: number[], fee: number) => number;

/**
* Accepted
*/
export const maxProfit: MaxProfit = (prices, fee) => {
let cash = 0;
let hold = -prices[0];

for (let i = 1; i < prices.length; i++) {
cash = Math.max(cash, hold + prices[i] - fee);
hold = Math.max(hold, cash - prices[i]);
}

return cash;
};

/**
* Accepted
*/
export const maxProfit2: MaxProfit = (prices, fee) => {
let profit = 0;
let prevPrice = prices[0];

for (let i = 0; i < prices.length; i++) {
if (prices[i] > prevPrice + fee) {
profit += prices[i] - prevPrice - fee;
prevPrice = prices[i] - fee;
}

if (prices[i] < prevPrice) {
prevPrice = prices[i];
}
}

return profit;
};

0 comments on commit d4a3e3d

Please sign in to comment.