From 49b570fdccd9c1647f6013772db4a38bd83530b5 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Tue, 24 Dec 2024 11:23:56 +0800 Subject: [PATCH] 258th Commit --- .../README.md | 17 ++++++++++++----- .../maxProfit.ts | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md index 4112370..48a598b 100644 --- a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md +++ b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/README.md @@ -104,19 +104,26 @@ export const maxProfit: MaxProfit = (prices, fee) => { 使用貪婪 (Greedy) +決策: + +- 何時賣出:當當前價格與之前的買入價格之差超過交易費時賣出,以鎖定利潤。 +- 何時更新「買入價」:如果找到比目前追踪的 `prevPrice` 更低的價格,就更新 `prevPrice`,以便減少成本。 + ```ts export const maxProfit2: MaxProfit = (prices, fee) => { - let profit = 0; - let prevPrice = prices[0]; + let profit = 0; // 總利潤 + let prevPrice = prices[0]; // 追踪有效的「買入價」 - for (let i = 0; i < prices.length; i++) { + for (let i = 1; i < prices.length; i++) { + // 如果達到賣出的利潤門檻 if (prices[i] > prevPrice + fee) { profit += prices[i] - prevPrice - fee; - prevPrice = prices[i] - fee; + prevPrice = prices[i] - fee; // 更新「買入價」以便後續交易 } + // 如果當前價格低於「買入價」 if (prices[i] < prevPrice) { - prevPrice = prices[i]; + prevPrice = prices[i]; // 更新「買入價」 } } diff --git a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/maxProfit.ts b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/maxProfit.ts index 8e39ce9..e6e0b54 100644 --- a/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/maxProfit.ts +++ b/src/page-7/714. Best Time to Buy and Sell Stock with Transaction Fee/maxProfit.ts @@ -22,7 +22,7 @@ export const maxProfit2: MaxProfit = (prices, fee) => { let profit = 0; let prevPrice = prices[0]; - for (let i = 0; i < prices.length; i++) { + for (let i = 1; i < prices.length; i++) { if (prices[i] > prevPrice + fee) { profit += prices[i] - prevPrice - fee; prevPrice = prices[i] - fee;