-
Notifications
You must be signed in to change notification settings - Fork 0
/
65.BestTimetoBuyandStockwithTransactionFee.java
64 lines (52 loc) · 1.98 KB
/
65.BestTimetoBuyandStockwithTransactionFee.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution {
public int maxProfit(int[] prices, int fee) {
int profit = 0; // Initialize profit to 0
int minPrice = prices[0]; // Initialize minPrice to the first price
// Iterate over the prices starting from the second element
for (int i = 1; i < prices.length; i++) {
int price = prices[i]; // Current price
// If the current price is less than minPrice, update minPrice
if (price < minPrice) {
minPrice = price;
}
// If the current price is greater than minPrice + fee, there's a potential profit
if (price > minPrice + fee) {
// Calculate the profit and add it to the total profit
profit += price - minPrice - fee;
// Update minPrice to the current price - fee (to account for selling)
minPrice = price - fee;
}
}
return profit; // Return the total profit
}
}
/*
class Solution {
int dp[][] = new int[50001][2];
int FEE;
public int maxProfit(int[] prices, int fee) {
int n = prices.length;
FEE = fee;
for (int[] row : dp) {
Arrays.fill(row, -1);
}
boolean buy = true;
return solve(prices, 0, n, buy);
}
public int solve(int[] prices, int day, int n, boolean buy) {
if (day >= n) return 0;
if (dp[day][buy ? 1 : 0] != -1) return dp[day][buy ? 1 : 0];
int profit = 0;
if (buy) {
int take = solve(prices, day + 1, n, false) - prices[day] - FEE;
int not_take = solve(prices, day + 1, n, true);
profit = Math.max(take, not_take);
} else {
int sell = prices[day] + solve(prices, day + 1, n, true);
int not_sell = solve(prices, day + 1, n, false);
profit = Math.max(sell, not_sell);
}
return dp[day][buy ? 1 : 0] = profit;
}
}
*/