Skip to content

Commit d5bb296

Browse files
committed
add 714 jaav
1 parent af3b013 commit d5bb296

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@
10791079
673 | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/) | [Python](./leetcode_python//Dynamic_Programming/number-of-longest-increasing-subsequence.py) | _O(n^2)_ | _O(n)_ | Medium |`dp`,`good trick`, `fb`| AGAIN********** (3)
10801080
688 | [Knight Probability in Chessboard](https://leetcode.com/problems/knight-probability-in-chessboard/) | [Python](./leetcode_python//Dynamic_Programming/knight-probability-in-chessboard.py) | _O(k * n^2)_ | _O(n^2)_ | Medium |`dp`,`dp basic`,`AGAIN`, `M$`,`Goldman Sachs`, `google`, `fb`, `amazon`| AGAIN********* (5)
10811081
712 | [Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [Python](./leetcode_python//Dynamic_Programming/minimum-ascii-delete-sum-for-two-strings.py) | _O(m * n)_ | _O(n)_ | Medium |`dp`| AGAIN (not start)
1082-
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/) | [Python](./leetcode_python//Dynamic_Programming/best-time-to-buy-and-sell-stock-with-transaction-fee.py) | _O(n)_ | _O(1)_ | Medium |AGAIN, `good basic`, `dp`, `greedy`,`fb`| AGAIN******** (3)
1082+
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/) | [Python](./leetcode_python//Dynamic_Programming/best-time-to-buy-and-sell-stock-with-transaction-fee.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/BestTimeToBuyAndSellStockWithTransactionFee.java) | _O(n)_ | _O(1)_ | Medium |AGAIN, `good basic`, `dp`, `greedy`,`fb`, google| AGAIN******** (3)
10831083
727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence/description/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MinimumWindowSubsequence.java) | _O(n)_ | _O(1)_ | Hard |`dp`,`google`| AGAIN (not start)
10841084
740 | [Delete and Earn](https://leetcode.com/problems/delete-and-earn/) | [Python](./leetcode_python//Dynamic_Programming/delete-and-earn.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
10851085
746 | [Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [Python](./leetcode_python//Dynamic_Programming/min-cost-climbing-stairs.py) | _O(n)_ | _O(1)_ | Easy |`good dp basic`,`dp`, `amazon`| AGAIN**** (2)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package LeetCodeJava.DynamicProgramming;
2+
3+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
4+
/**
5+
* 714. Best Time to Buy and Sell Stock with Transaction Fee
6+
* Medium
7+
* Topics
8+
* Companies
9+
* Hint
10+
* 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.
11+
*
12+
* 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.
13+
*
14+
* Note:
15+
*
16+
* You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
17+
* The transaction fee is only charged once for each stock purchase and sale.
18+
*
19+
*
20+
* Example 1:
21+
*
22+
* Input: prices = [1,3,2,8,4,9], fee = 2
23+
* Output: 8
24+
* Explanation: The maximum profit can be achieved by:
25+
* - Buying at prices[0] = 1
26+
* - Selling at prices[3] = 8
27+
* - Buying at prices[4] = 4
28+
* - Selling at prices[5] = 9
29+
* The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
30+
* Example 2:
31+
*
32+
* Input: prices = [1,3,7,5,10,3], fee = 3
33+
* Output: 6
34+
*
35+
*
36+
* Constraints:
37+
*
38+
* 1 <= prices.length <= 5 * 104
39+
* 1 <= prices[i] < 5 * 104
40+
* 0 <= fee < 5 * 104
41+
*
42+
*
43+
*/
44+
public class BestTimeToBuyAndSellStockWithTransactionFee {
45+
46+
// V0
47+
// public int maxProfit(int[] prices, int fee) {
48+
//
49+
// }
50+
51+
// V1
52+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/3667440/beats-100-c-java-python-beginner-friendl-rpgh/
53+
public int maxProfit_1(int[] prices, int fee) {
54+
int buy = Integer.MIN_VALUE;
55+
int sell = 0;
56+
57+
for (int price : prices) {
58+
buy = Math.max(buy, sell - price);
59+
sell = Math.max(sell, buy + price - fee);
60+
}
61+
62+
return sell;
63+
}
64+
65+
// V2
66+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/1112088/js-python-java-c-very-simple-state-machi-f4zp/
67+
public int maxProfit_2(int[] P, int F) {
68+
int len = P.length, buying = 0, selling = -P[0];
69+
for (int i = 1; i < len; i++) {
70+
buying = Math.max(buying, selling + P[i] - F);
71+
selling = Math.max(selling, buying - P[i]);
72+
}
73+
return buying;
74+
}
75+
76+
// V3
77+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/1600084/dp-state-machine-top-down-to-bottom-up-w-vdzi/
78+
79+
// V4
80+
// IDEA: DP
81+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/3667495/beats-100-video-java-c-python-by-jeevank-uyrl/
82+
public int maxProfit_4(int[] prices, int fee) {
83+
int free = 0;
84+
int hold = -prices[0];
85+
for (int i : prices) {
86+
int tmp = hold;
87+
hold = Math.max(hold, free - i);
88+
free = Math.max(free, tmp + i - fee);
89+
}
90+
return free;
91+
}
92+
93+
}

0 commit comments

Comments
 (0)