Skip to content

Commit 42575ee

Browse files
committed
update 714 java
1 parent d5bb296 commit 42575ee

File tree

3 files changed

+242
-4
lines changed

3 files changed

+242
-4
lines changed

doc/cheatsheet/dp.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ problems by combining the solutions to subproblems
1111
- https://predoc.dlc.ntu.edu.tw/viewer?embedded=true&url=https%3A%2F%2Fcool.ntu.edu.tw%2Fcourses%2F8583%2Ffiles%2F1165602%2Fdownload%3Fverifier%3DnlJ3s1a9TTmgYQzbJgj9vnrGlKKZB4w0wUZyEKgm
1212

1313
### 0-1) Types
14+
- 2 DP state:
15+
- LC 714
1416

1517
### 0-2) Pattern
1618

@@ -185,4 +187,98 @@ private int findMin(int a, int b, int c){
185187
return c;
186188
}
187189
}
190+
```
191+
192+
### 2-3) Best Time to Buy and Sell Stock with Transaction Fee
193+
194+
```java
195+
// java
196+
// LC 714
197+
198+
// V0-1
199+
// IDEA: DP (gpt)
200+
/**
201+
* Solution Explanation:
202+
*
203+
*
204+
* - Use two variables to represent the state:
205+
* 1. hold: The maximum profit achievable
206+
* while holding a stock at day i.
207+
*
208+
* 2. cash: The maximum profit achievable
209+
* while not holding a stock at day i.
210+
*
211+
* - Transition equations:
212+
* - If holding a stock:
213+
* hold = max(hold, cash - price[i])
214+
*
215+
* NOTE: 2 cases we hold th stock: 1) already hold from previous day 2) buy a new stock today
216+
* (`hold`: You already held the stock from a previous day -> If you decided not to make any changes today, then the profit remains the same as the previous hold.)
217+
* (`cash - price[i]`: You buy the stock today -> To buy the stock today, you need to spend money, reducing your profit. The cost to buy the stock is prices[i]. However, the amount of money you can spend is the maximum profit you had when you were not holding a stock previously (cash).)
218+
*
219+
* (You either keep holding or buy a new stock.)
220+
* - If not holding a stock:
221+
* cash = max(cash, hold + price[i] - fee)
222+
*
223+
*
224+
* (You either keep not holding or sell the stock and pay the fee.)
225+
* - Initialize:
226+
* - hold = -prices[0] (If you buy the stock on the first day).
227+
* - cash = 0 (You haven’t made any transactions yet).
228+
*
229+
*/
230+
/**
231+
* Example Walkthrough:
232+
*
233+
* Input:
234+
* • Prices: [1, 3, 2, 8, 4, 9]
235+
* • Fee: 2
236+
*
237+
* Steps:
238+
* 1. Day 0:
239+
* • hold = -1 (Buy the stock at price 1).
240+
* • cash = 0.
241+
* 2. Day 1:
242+
* • cash = max(0, -1 + 3 - 2) = 0 (No selling since profit is 0).
243+
* • hold = max(-1, 0 - 3) = -1 (No buying since it’s already better to hold).
244+
* 3. Day 2:
245+
* • cash = max(0, -1 + 2 - 2) = 0.
246+
* • hold = max(-1, 0 - 2) = -1.
247+
* 4. Day 3:
248+
* • cash = max(0, -1 + 8 - 2) = 5 (Sell at price 8).
249+
* • hold = max(-1, 5 - 8) = -1.
250+
* 5. Day 4:
251+
* • cash = max(5, -1 + 4 - 2) = 5.
252+
* • hold = max(-1, 5 - 4) = 1.
253+
* 6. Day 5:
254+
* • cash = max(5, 1 + 9 - 2) = 8 (Sell at price 9).
255+
* • hold = max(1, 5 - 9) = 1.
256+
*
257+
* Output:
258+
* • cash = 8 (Max profit).
259+
*
260+
*/
261+
public int maxProfit_0_1(int[] prices, int fee) {
262+
// Edge case
263+
if (prices == null || prices.length == 0) {
264+
return 0;
265+
}
266+
267+
// Initialize states
268+
int hold = -prices[0]; // Maximum profit when holding a stock
269+
int cash = 0; // Maximum profit when not holding a stock
270+
271+
// Iterate through prices
272+
for (int i = 1; i < prices.length; i++) {
273+
/**
274+
* NOTE !!! there are 2 dp equations (e.g. cash, hold)
275+
*/
276+
// Update cash and hold states
277+
cash = Math.max(cash, hold + prices[i] - fee); // Sell the stock
278+
hold = Math.max(hold, cash - prices[i]); // Buy the stock
279+
}
280+
281+
// The maximum profit at the end is when not holding any stock
282+
return cash;
283+
}
188284
```

leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/BestTimeToBuyAndSellStockWithTransactionFee.java

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,97 @@
4343
*/
4444
public class BestTimeToBuyAndSellStockWithTransactionFee {
4545

46-
// V0
47-
// public int maxProfit(int[] prices, int fee) {
48-
//
49-
// }
46+
// V0
47+
// public int maxProfit(int[] prices, int fee) {
48+
//
49+
// }
50+
51+
// V0-1
52+
// IDEA: DP (gpt)
53+
/**
54+
* Solution Explanation:
55+
*
56+
*
57+
* - Use two variables to represent the state:
58+
* 1. hold: The maximum profit achievable
59+
* while holding a stock at day i.
60+
*
61+
* 2. cash: The maximum profit achievable
62+
* while not holding a stock at day i.
63+
*
64+
* - Transition equations:
65+
* - If holding a stock:
66+
* hold = max(hold, cash - price[i])
67+
*
68+
* NOTE: 2 cases we hold th stock: 1) already hold from previous day 2) buy a new stock today
69+
* (`hold`: You already held the stock from a previous day -> If you decided not to make any changes today, then the profit remains the same as the previous hold.)
70+
* (`cash - price[i]`: You buy the stock today -> To buy the stock today, you need to spend money, reducing your profit. The cost to buy the stock is prices[i]. However, the amount of money you can spend is the maximum profit you had when you were not holding a stock previously (cash).)
71+
*
72+
* (You either keep holding or buy a new stock.)
73+
* - If not holding a stock:
74+
* cash = max(cash, hold + price[i] - fee)
75+
*
76+
*
77+
* (You either keep not holding or sell the stock and pay the fee.)
78+
* - Initialize:
79+
* - hold = -prices[0] (If you buy the stock on the first day).
80+
* - cash = 0 (You haven’t made any transactions yet).
81+
*
82+
*/
83+
/**
84+
* Example Walkthrough:
85+
*
86+
* Input:
87+
* • Prices: [1, 3, 2, 8, 4, 9]
88+
* • Fee: 2
89+
*
90+
* Steps:
91+
* 1. Day 0:
92+
* • hold = -1 (Buy the stock at price 1).
93+
* • cash = 0.
94+
* 2. Day 1:
95+
* • cash = max(0, -1 + 3 - 2) = 0 (No selling since profit is 0).
96+
* • hold = max(-1, 0 - 3) = -1 (No buying since it’s already better to hold).
97+
* 3. Day 2:
98+
* • cash = max(0, -1 + 2 - 2) = 0.
99+
* • hold = max(-1, 0 - 2) = -1.
100+
* 4. Day 3:
101+
* • cash = max(0, -1 + 8 - 2) = 5 (Sell at price 8).
102+
* • hold = max(-1, 5 - 8) = -1.
103+
* 5. Day 4:
104+
* • cash = max(5, -1 + 4 - 2) = 5.
105+
* • hold = max(-1, 5 - 4) = 1.
106+
* 6. Day 5:
107+
* • cash = max(5, 1 + 9 - 2) = 8 (Sell at price 9).
108+
* • hold = max(1, 5 - 9) = 1.
109+
*
110+
* Output:
111+
* • cash = 8 (Max profit).
112+
*
113+
*/
114+
public int maxProfit_0_1(int[] prices, int fee) {
115+
// Edge case
116+
if (prices == null || prices.length == 0) {
117+
return 0;
118+
}
119+
120+
// Initialize states
121+
int hold = -prices[0]; // Maximum profit when holding a stock
122+
int cash = 0; // Maximum profit when not holding a stock
123+
124+
// Iterate through prices
125+
for (int i = 1; i < prices.length; i++) {
126+
/**
127+
* NOTE !!! there are 2 dp equations (e.g. cash, hold)
128+
*/
129+
// Update cash and hold states
130+
cash = Math.max(cash, hold + prices[i] - fee); // Sell the stock
131+
hold = Math.max(hold, cash - prices[i]); // Buy the stock
132+
}
133+
134+
// The maximum profit at the end is when not holding any stock
135+
return cash;
136+
}
50137

51138
// V1
52139
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/3667440/beats-100-c-java-python-beginner-friendl-rpgh/

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,61 @@ public String multiply(String num1, String num2) {
27732773
return null;
27742774
}
27752775

2776+
// LC 714
2777+
// 7.17 pm - 7.30 pm
2778+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
2779+
/**
2780+
*
2781+
*
2782+
* IDEA: DP
2783+
*
2784+
*
2785+
* 1. transaction fee need to be paid at EVERY sale
2786+
* 2.
2787+
*
2788+
*
2789+
* Exp 1)
2790+
*
2791+
* Input: prices = [1,3,2,8,4,9], fee = 2
2792+
* -> Output: 8
2793+
*
2794+
*
2795+
* [1,3,2,8,4,9], fee = 2
2796+
* b s (rev = (3-1-2) = 0)
2797+
* b s (rev = (8-1-2) = 5
2798+
*
2799+
*/
2800+
public int maxProfit(int[] prices, int fee) {
2801+
2802+
int res = 0;
2803+
// edge
2804+
if (prices.length == 1){
2805+
return 0;
2806+
}
2807+
if(prices.length == 2){
2808+
if(prices[1] > prices[0]){
2809+
return Math.max(0, prices[1] - prices[0] - fee);
2810+
}
2811+
}
2812+
2813+
// dp
2814+
int[] dp = new int[prices.length+1];
2815+
// fill dp with 0
2816+
Arrays.fill(dp, 0); // ??
2817+
// init state
2818+
dp[0] = 0;
2819+
if(prices[1] > prices[0]){
2820+
dp[1] = Math.max(0, prices[1] - prices[0] - fee);
2821+
}
2822+
2823+
// 2 option:
2824+
// if not buy: buy or not buy
2825+
// if already buy: sale or hold
2826+
2827+
2828+
return res;
2829+
}
2830+
27762831

27772832

27782833
}

0 commit comments

Comments
 (0)