From ce17f91d32ed22d6a60dd023ff4e6d330be1e426 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:26:14 +0530 Subject: [PATCH 1/6] Create 714. Best Time to Buy and Sell Stock with Transaction Fee.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../714. Best Time to Buy and Sell Stock with Transaction Fee.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py new file mode 100644 index 0000000..e69de29 From 33af296bff8eb39d9b04402359a483a093fe27d5 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:26:57 +0530 Subject: [PATCH 2/6] Update 714. Best Time to Buy and Sell Stock with Transaction Fee.py Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- ...e to Buy and Sell Stock with Transaction Fee.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py index e69de29..c44ea51 100644 --- a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py +++ b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.py @@ -0,0 +1,14 @@ +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + @cache + def dfs(i: int, j: int) -> int: + if i >= len(prices): + return 0 + ans = dfs(i + 1, j) + if j: + ans = max(ans, prices[i] + dfs(i + 1, 0) - fee) + else: + ans = max(ans, -prices[i] + dfs(i + 1, 1)) + return ans + + return dfs(0, 0) \ No newline at end of file From 83050677538237eadf3c55cd1d7611f3896995d0 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:27:34 +0530 Subject: [PATCH 3/6] Create readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md new file mode 100644 index 0000000..e69de29 From df19d2c4de85bb18b4cb2a145f49e3a0755efba9 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:27:41 +0530 Subject: [PATCH 4/6] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 403 ++++++++++++++++++ 1 file changed, 403 insertions(+) diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md index e69de29..dc93e95 100644 --- a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md +++ b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md @@ -0,0 +1,403 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README_EN.md +tags: + - Greedy + - Array + - Dynamic Programming +--- + + + +# [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) + +[中文文档](/solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README.md) + +## Description + + + +

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

+ +

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

+ +

Note:

+ + + +

 

+

Example 1:

+ +
+Input: prices = [1,3,2,8,4,9], fee = 2
+Output: 8
+Explanation: The maximum profit can be achieved by:
+- Buying at prices[0] = 1
+- Selling at prices[3] = 8
+- Buying at prices[4] = 4
+- Selling at prices[5] = 9
+The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
+
+ +

Example 2:

+ +
+Input: prices = [1,3,7,5,10,3], fee = 3
+Output: 6
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Memoization + +We design a function $dfs(i, j)$, which represents the maximum profit that can be obtained starting from day $i$ with state $j$. Here, $j$ can take the values $0$ and $1$, representing not holding and holding a stock, respectively. The answer is $dfs(0, 0)$. + +The execution logic of the function $dfs(i, j)$ is as follows: + +If $i \geq n$, there are no more stocks to trade, so we return $0$. + +Otherwise, we can choose not to trade, in which case $dfs(i, j) = dfs(i + 1, j)$. We can also choose to trade stocks. If $j \gt 0$, it means that we currently hold a stock and can sell it. In this case, $dfs(i, j) = prices[i] + dfs(i + 1, 0) - fee$. If $j = 0$, it means that we currently do not hold a stock and can buy one. In this case, $dfs(i, j) = -prices[i] + dfs(i + 1, 1)$. We take the maximum value as the return value of the function $dfs(i, j)$. + +The answer is $dfs(0, 0)$. + +To avoid redundant calculations, we use memoization to record the return value of $dfs(i, j)$ in an array $f$. If $f[i][j]$ is not equal to $-1$, it means that we have already calculated it, so we can directly return $f[i][j]$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $prices$. + + + +#### Python3 + +```python +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + @cache + def dfs(i: int, j: int) -> int: + if i >= len(prices): + return 0 + ans = dfs(i + 1, j) + if j: + ans = max(ans, prices[i] + dfs(i + 1, 0) - fee) + else: + ans = max(ans, -prices[i] + dfs(i + 1, 1)) + return ans + + return dfs(0, 0) +``` + +#### Java + +```java +class Solution { + private Integer[][] f; + private int[] prices; + private int fee; + + public int maxProfit(int[] prices, int fee) { + f = new Integer[prices.length][2]; + this.prices = prices; + this.fee = fee; + return dfs(0, 0); + } + + private int dfs(int i, int j) { + if (i >= prices.length) { + return 0; + } + if (f[i][j] != null) { + return f[i][j]; + } + int ans = dfs(i + 1, j); + if (j > 0) { + ans = Math.max(ans, prices[i] + dfs(i + 1, 0) - fee); + } else { + ans = Math.max(ans, -prices[i] + dfs(i + 1, 1)); + } + return f[i][j] = ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxProfit(vector& prices, int fee) { + int n = prices.size(); + int f[n][2]; + memset(f, -1, sizeof(f)); + function dfs = [&](int i, int j) { + if (i >= prices.size()) { + return 0; + } + if (f[i][j] != -1) { + return f[i][j]; + } + int ans = dfs(i + 1, j); + if (j) { + ans = max(ans, prices[i] + dfs(i + 1, 0) - fee); + } else { + ans = max(ans, -prices[i] + dfs(i + 1, 1)); + } + return f[i][j] = ans; + }; + return dfs(0, 0); + } +}; +``` + +#### Go + +```go +func maxProfit(prices []int, fee int) int { + n := len(prices) + f := make([][2]int, n) + for i := range f { + f[i] = [2]int{-1, -1} + } + var dfs func(i, j int) int + dfs = func(i, j int) int { + if i >= n { + return 0 + } + if f[i][j] != -1 { + return f[i][j] + } + ans := dfs(i+1, j) + if j > 0 { + ans = max(ans, prices[i]+dfs(i+1, 0)-fee) + } else { + ans = max(ans, -prices[i]+dfs(i+1, 1)) + } + f[i][j] = ans + return ans + } + return dfs(0, 0) +} +``` + +#### TypeScript + +```ts +function maxProfit(prices: number[], fee: number): number { + const n = prices.length; + const f: number[][] = Array.from({ length: n }, () => [-1, -1]); + const dfs = (i: number, j: number): number => { + if (i >= n) { + return 0; + } + if (f[i][j] !== -1) { + return f[i][j]; + } + let ans = dfs(i + 1, j); + if (j) { + ans = Math.max(ans, prices[i] + dfs(i + 1, 0) - fee); + } else { + ans = Math.max(ans, -prices[i] + dfs(i + 1, 1)); + } + return (f[i][j] = ans); + }; + return dfs(0, 0); +} +``` + + + + + + + +### Solution 2: Dynamic Programming + +We define $f[i][j]$ as the maximum profit that can be obtained up to day $i$ with state $j$. Here, $j$ can take the values $0$ and $1$, representing not holding and holding a stock, respectively. We initialize $f[0][0] = 0$ and $f[0][1] = -prices[0]$. + +When $i \geq 1$, if we do not hold a stock at the current day, then $f[i][0]$ can be obtained by transitioning from $f[i - 1][0]$ and $f[i - 1][1] + prices[i] - fee$, i.e., $f[i][0] = \max(f[i - 1][0], f[i - 1][1] + prices[i] - fee)$. If we hold a stock at the current day, then $f[i][1]$ can be obtained by transitioning from $f[i - 1][1]$ and $f[i - 1][0] - prices[i]$, i.e., $f[i][1] = \max(f[i - 1][1], f[i - 1][0] - prices[i])$. The final answer is $f[n - 1][0]$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $prices$. + +We notice that the transition of the state $f[i][]$ only depends on $f[i - 1][]$ and $f[i - 2][]$. Therefore, we can use two variables $f_0$ and $f_1$ to replace the array $f$, reducing the space complexity to $O(1)$. + + + +#### Python3 + +```python +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + n = len(prices) + f = [[0] * 2 for _ in range(n)] + f[0][1] = -prices[0] + for i in range(1, n): + f[i][0] = max(f[i - 1][0], f[i - 1][1] + prices[i] - fee) + f[i][1] = max(f[i - 1][1], f[i - 1][0] - prices[i]) + return f[n - 1][0] +``` + +#### Java + +```java +class Solution { + public int maxProfit(int[] prices, int fee) { + int n = prices.length; + int[][] f = new int[n][2]; + f[0][1] = -prices[0]; + for (int i = 1; i < n; ++i) { + f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + prices[i] - fee); + f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]); + } + return f[n - 1][0]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxProfit(vector& prices, int fee) { + int n = prices.size(); + int f[n][2]; + memset(f, 0, sizeof(f)); + f[0][1] = -prices[0]; + for (int i = 1; i < n; ++i) { + f[i][0] = max(f[i - 1][0], f[i - 1][1] + prices[i] - fee); + f[i][1] = max(f[i - 1][1], f[i - 1][0] - prices[i]); + } + return f[n - 1][0]; + } +}; +``` + +#### Go + +```go +func maxProfit(prices []int, fee int) int { + n := len(prices) + f := make([][2]int, n) + f[0][1] = -prices[0] + for i := 1; i < n; i++ { + f[i][0] = max(f[i-1][0], f[i-1][1]+prices[i]-fee) + f[i][1] = max(f[i-1][1], f[i-1][0]-prices[i]) + } + return f[n-1][0] +} +``` + +#### TypeScript + +```ts +function maxProfit(prices: number[], fee: number): number { + const n = prices.length; + const f: number[][] = Array.from({ length: n }, () => [0, 0]); + f[0][1] = -prices[0]; + for (let i = 1; i < n; ++i) { + f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + prices[i] - fee); + f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]); + } + return f[n - 1][0]; +} +``` + + + + + + + +### Solution 3 + + + +#### Python3 + +```python +class Solution: + def maxProfit(self, prices: List[int], fee: int) -> int: + f0, f1 = 0, -prices[0] + for x in prices[1:]: + f0, f1 = max(f0, f1 + x - fee), max(f1, f0 - x) + return f0 +``` + +#### Java + +```java +class Solution { + public int maxProfit(int[] prices, int fee) { + int f0 = 0, f1 = -prices[0]; + for (int i = 1; i < prices.length; ++i) { + int g0 = Math.max(f0, f1 + prices[i] - fee); + f1 = Math.max(f1, f0 - prices[i]); + f0 = g0; + } + return f0; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxProfit(vector& prices, int fee) { + int f0 = 0, f1 = -prices[0]; + for (int i = 1; i < prices.size(); ++i) { + int g0 = max(f0, f1 + prices[i] - fee); + f1 = max(f1, f0 - prices[i]); + f0 = g0; + } + return f0; + } +}; +``` + +#### Go + +```go +func maxProfit(prices []int, fee int) int { + f0, f1 := 0, -prices[0] + for _, x := range prices[1:] { + f0, f1 = max(f0, f1+x-fee), max(f1, f0-x) + } + return f0 +} +``` + +#### TypeScript + +```ts +function maxProfit(prices: number[], fee: number): number { + const n = prices.length; + let [f0, f1] = [0, -prices[0]]; + for (const x of prices.slice(1)) { + [f0, f1] = [Math.max(f0, f1 + x - fee), Math.max(f1, f0 - x)]; + } + return f0; +} +``` + + + + + + \ No newline at end of file From 4ca237cfe2dbc5e4cf25c56f7d625f437deab47e Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:28:00 +0530 Subject: [PATCH 5/6] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md index dc93e95..a16ef7a 100644 --- a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md +++ b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md @@ -1,3 +1,8 @@ + + + +# [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) + --- comments: true difficulty: Medium @@ -8,12 +13,6 @@ tags: - Dynamic Programming --- - - -# [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) - -[中文文档](/solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README.md) - ## Description From a23653165da78132bad438bcc1935aecf5b3554b Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:28:56 +0530 Subject: [PATCH 6/6] Update readme.md Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com> --- .../readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md index a16ef7a..228ccd8 100644 --- a/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md +++ b/Solution/714. Best Time to Buy and Sell Stock with Transaction Fee/readme.md @@ -4,10 +4,9 @@ # [714. Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) --- -comments: true -difficulty: Medium -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0714.Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Transaction%20Fee/README_EN.md -tags: +- **comments**: true +- **difficulty**: Medium +- **tags**: - Greedy - Array - Dynamic Programming