-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path122. Best Time to Buy and Sell Stock II.java
75 lines (66 loc) · 2.6 KB
/
122. Best Time to Buy and Sell Stock II.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
65
66
67
68
69
70
71
72
73
74
75
class Solution {
public int maxProfit2(int[] arr) {
// BEST APPROACH::
// EASY: O(N) solution
//
final int n = arr.length;
int overrallProfit = 0;
for (int i = 1; i < n; i++) {
int profit = arr[i] - arr[i-1];
if (profit > 0) {
overrallProfit += profit;
}
}
return overrallProfit;
}
public int maxProfit(int[] prices) {
// return recursive(prices, 0, true);
int n = prices.length;
int[][] dp = new int[n + 1][2];
// base case n == prices.length
dp[n][0] = dp[n][1] = 0;
// fill the dp.
for (int i = n - 1; i >= 0; i--) {
for (int buy = 0; buy < 2; buy++) {
// buy : true
if (buy == 1) {
// Math.max((-1 * prices[i]) + recursive(prices, i + 1, false),
// recursive(prices, i + 1, true));
dp[i][buy] = Math.max(
(-1 * prices[i]) + dp[i + 1][0],
dp[i + 1][1]);
} else {
// sell : false
// if i sell, i will get the current price
// op2 = prices[i] + recursive(prices, i + 1, true);
// // if i skip, i will not get anything
// op3 = recursive(prices, i + 1, false);
dp[i][buy] = Math.max(
prices[i] + dp[i + 1][1],
dp[i + 1][0]);
}
}
}
return dp[0][1];
}
public int recursive(int[] prices, int i, boolean askToBuy) {
if (i == prices.length) {
return 0;
}
int op1 = 0, op2 = 0, op3 = 0;
// if asked to buy, i need to buy the current cost
if (askToBuy) {
// i am paying from my pocket
op1 = Math.max((-1 * prices[i]) + recursive(prices, i + 1, false), recursive(prices, i+1, true));
} else {
// as single is -> i can sell or skip it
// if i sell, i will get the current price
op2 = prices[i] + recursive(prices, i + 1, true);
// if i skip, i will not get anything
op3 = recursive(prices, i + 1, false);
}
return Math.max(op1, Math.max(op2, op3));
}
}