Skip to content

Commit 3d4a581

Browse files
committed
leetcode
1 parent a2cd2d6 commit 3d4a581

4 files changed

+310
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
3+
4+
-* 714. Best Time to Buy and Sell Stock with Transaction Fee *-
5+
6+
7+
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.
8+
9+
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.
10+
11+
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
12+
13+
14+
15+
Example 1:
16+
17+
Input: prices = [1,3,2,8,4,9], fee = 2
18+
Output: 8
19+
Explanation: The maximum profit can be achieved by:
20+
- Buying at prices[0] = 1
21+
- Selling at prices[3] = 8
22+
- Buying at prices[4] = 4
23+
- Selling at prices[5] = 9
24+
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
25+
Example 2:
26+
27+
Input: prices = [1,3,7,5,10,3], fee = 3
28+
Output: 6
29+
30+
31+
Constraints:
32+
33+
1 <= prices.length <= 5 * 104
34+
1 <= prices[i] < 5 * 104
35+
0 <= fee < 5 * 104
36+
37+
*/
38+
39+
import 'dart:math';
40+
41+
class A {
42+
int maxProfit(List<int> prices, int fee) {
43+
int buy = -2147483648;
44+
int sell = 0;
45+
46+
for (int price in prices) {
47+
buy = buy > sell - price ? buy : sell - price;
48+
sell = sell > buy + price - fee ? sell : buy + price - fee;
49+
}
50+
51+
return sell;
52+
}
53+
}
54+
55+
class B {
56+
int getAns(
57+
List<int> arr, int ind, int buy, int n, int fee, List<List<int>> dp) {
58+
if (ind == n) return 0; // base case
59+
60+
if (dp[ind][buy] != -1) return dp[ind][buy];
61+
62+
int profit = 0;
63+
64+
if (buy == 0) {
65+
// We can buy the stock
66+
profit = max(
67+
0 + getAns(arr, ind + 1, 0, n, fee, dp),
68+
-arr[ind] + getAns(arr, ind + 1, 1, n, fee, dp),
69+
);
70+
}
71+
72+
if (buy == 1) {
73+
// We can sell the stock
74+
profit = max(
75+
0 + getAns(arr, ind + 1, 1, n, fee, dp),
76+
arr[ind] - fee + getAns(arr, ind + 1, 0, n, fee, dp),
77+
);
78+
}
79+
80+
return dp[ind][buy] = profit;
81+
}
82+
83+
int maxProfit(List<int> prices, int fee) {
84+
List<List<int>> dp =
85+
List.generate(prices.length, (_) => List<int>.filled(2, -1));
86+
87+
if (prices.length == 0) return 0;
88+
89+
int ans = getAns(prices, 0, 0, prices.length, fee, dp);
90+
return ans;
91+
}
92+
}
93+
94+
class C {
95+
int maxProfit(List<int> prices, int fee) {
96+
if (prices.isEmpty) return 0;
97+
int n = prices.length;
98+
99+
List<List<int>> dp = List.generate(n, (_) => List<int>.filled(2, 0));
100+
101+
// Initialize base cases
102+
dp[0][0] = 0;
103+
dp[0][1] = -prices[0];
104+
105+
for (int i = 1; i < n; i++) {
106+
// Calculate the maximum profit when not holding a stock
107+
dp[i][0] = dp[i - 1][0];
108+
if (dp[i - 1][1] + prices[i] - fee > dp[i][0]) {
109+
dp[i][0] = dp[i - 1][1] + prices[i] - fee;
110+
}
111+
112+
// Calculate the maximum profit when holding a stock
113+
dp[i][1] = dp[i - 1][1];
114+
if (dp[i - 1][0] - prices[i] > dp[i][1]) {
115+
dp[i][1] = dp[i - 1][0] - prices[i];
116+
}
117+
}
118+
119+
return dp[n - 1][0];
120+
}
121+
}
122+
123+
class D {
124+
int maxProfit(List<int> prices, int fee) {
125+
int n = prices.length;
126+
if (n == 0) return 0;
127+
128+
Map<String, int> dp = {};
129+
130+
// Base cases
131+
dp['false-$n'] = 0;
132+
dp['true-$n'] = 0;
133+
134+
for (int i = n - 1; i >= 0; i--) {
135+
for (bool buy in [false, true]) {
136+
if (buy) {
137+
dp['$buy-$i'] = ((dp['false-${i + 1}']! + prices[i])
138+
.compareTo(dp['$buy-${i + 1}']!) >
139+
0
140+
? dp['false-${i + 1}']! + prices[i]
141+
: dp['$buy-${i + 1}'])!;
142+
} else {
143+
dp['$buy-$i'] = ((dp['$buy-${i + 1}'])!
144+
.compareTo(dp['true-${i + 1}']! - prices[i] - fee) >
145+
0
146+
? dp['$buy-${i + 1}']
147+
: dp['true-${i + 1}']! - prices[i] - fee)!;
148+
}
149+
}
150+
}
151+
152+
return dp['false-0']!;
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
func maxProfit(prices []int, fee int) int {
4+
if len(prices) == 0 {
5+
return 0
6+
}
7+
8+
n := len(prices)
9+
dp := make([][2]int, n)
10+
11+
// Initialize base cases
12+
dp[0][0] = 0
13+
dp[0][1] = -prices[0]
14+
15+
for i := 1; i < n; i++ {
16+
// Calculate the maximum profit when not holding a stock
17+
dp[i][0] = dp[i-1][0]
18+
if dp[i-1][1]+prices[i]-fee > dp[i][0] {
19+
dp[i][0] = dp[i-1][1] + prices[i] - fee
20+
}
21+
22+
// Calculate the maximum profit when holding a stock
23+
dp[i][1] = dp[i-1][1]
24+
if dp[i-1][0]-prices[i] > dp[i][1] {
25+
dp[i][1] = dp[i-1][0] - prices[i]
26+
}
27+
}
28+
29+
return dp[n-1][0]
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 🔥 3 Solutions 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
## Solution - 1
4+
5+
## Intuition
6+
7+
The intuition behind the solution is to keep track of the minimum cost to buy a stock at each day and the maximum profit that can be achieved by selling the stock at each day.
8+
9+
## Approach
10+
11+
Initialize two variables: buy and sell. Set buy to negative infinity and sell to zero. These variables will keep track of the maximum profit at each day.
12+
13+
Iterate through the prices of the stocks starting from the first day.
14+
15+
Update the buy variable by taking the maximum of its current value and the previous sell value minus the stock price. This represents the maximum profit after buying the stock.
16+
buy = max(buy, sell - price)
17+
18+
Update the sell variable by taking the maximum of its current value and the previous buy value plus the stock price minus the transaction fee. This represents the maximum profit after selling the stock.
19+
sell = max(sell, buy + price - fee)
20+
21+
After iterating through all the prices, the maximum profit will be stored in the sell variable.
22+
23+
Return the value of sell as the maximum profit.
24+
25+
## Complexity
26+
27+
### Time complexity
28+
29+
O(n)
30+
31+
### Space complexity
32+
33+
O(1)
34+
35+
```dart
36+
class Solution {
37+
int maxProfit(List<int> prices, int fee) {
38+
int buy = -2147483648;
39+
int sell = 0;
40+
41+
for (int price in prices) {
42+
buy = buy > sell - price ? buy : sell - price;
43+
sell = sell > buy + price - fee ? sell : buy + price - fee;
44+
}
45+
46+
return sell;
47+
}
48+
}
49+
```
50+
51+
## Solution - 2 -- Stack OverFLow (NOT Optimized)
52+
53+
```dart
54+
class B {
55+
int getAns(
56+
List<int> arr, int ind, int buy, int n, int fee, List<List<int>> dp) {
57+
if (ind == n) return 0; // base case
58+
59+
if (dp[ind][buy] != -1) return dp[ind][buy];
60+
61+
int profit = 0;
62+
63+
if (buy == 0) {
64+
// We can buy the stock
65+
profit = max(
66+
0 + getAns(arr, ind + 1, 0, n, fee, dp),
67+
-arr[ind] + getAns(arr, ind + 1, 1, n, fee, dp),
68+
);
69+
}
70+
71+
if (buy == 1) {
72+
// We can sell the stock
73+
profit = max(
74+
0 + getAns(arr, ind + 1, 1, n, fee, dp),
75+
arr[ind] - fee + getAns(arr, ind + 1, 0, n, fee, dp),
76+
);
77+
}
78+
79+
return dp[ind][buy] = profit;
80+
}
81+
82+
int maxProfit(List<int> prices, int fee) {
83+
List<List<int>> dp =
84+
List.generate(prices.length, (_) => List<int>.filled(2, -1));
85+
86+
if (prices.length == 0) return 0;
87+
88+
int ans = getAns(prices, 0, 0, prices.length, fee, dp);
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
## Solution - 3 - Optimized
95+
96+
```dart
97+
class Solution {
98+
int maxProfit(List<int> prices, int fee) {
99+
if (prices.isEmpty) return 0;
100+
int n = prices.length;
101+
102+
List<List<int>> dp = List.generate(n, (_) => List<int>.filled(2, 0));
103+
104+
// Initialize base cases
105+
dp[0][0] = 0;
106+
dp[0][1] = -prices[0];
107+
108+
for (int i = 1; i < n; i++) {
109+
// Calculate the maximum profit when not holding a stock
110+
dp[i][0] = dp[i - 1][0];
111+
if (dp[i - 1][1] + prices[i] - fee > dp[i][0]) {
112+
dp[i][0] = dp[i - 1][1] + prices[i] - fee;
113+
}
114+
115+
// Calculate the maximum profit when holding a stock
116+
dp[i][1] = dp[i - 1][1];
117+
if (dp[i - 1][0] - prices[i] > dp[i][1]) {
118+
dp[i][1] = dp[i - 1][0] - prices[i];
119+
}
120+
}
121+
122+
return dp[n - 1][0];
123+
}
124+
}
125+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
235235
- [**1187.** Make Array Strictly Increasing](MakeArrayStrictlyIncreasing\make_array_strictly_increasing.dart)
236236
- [**1732.** Find the Highest Altitude](FindTheHighestAltitude/find_the_highest_altitude.dart)
237237
- [**2090.** K Radius SubArray Averages](KRadiusSubarrayAverages/k_radius_subarray_averages.dart)
238+
- [**714.** Best Time to Buy and Sell Stock with Transaction Fee](BestTimeToBuyAndSellStockWithTransactionFee/best_time_to_buy_and_sell_stock_with_transaction_fee.dart)
238239

239240
## Reach me via
240241

0 commit comments

Comments
 (0)