Skip to content

Commit dab63c3

Browse files
authored
Create Q-02: Best Time to Buy and Sell Stock II.java
1 parent 5afef48 commit dab63c3

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
122. Best Time to Buy and Sell Stock II
3+
4+
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
5+
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
6+
Find and return the maximum profit you can achieve.
7+
8+
Example 1:
9+
Input: prices = [7,1,5,3,6,4]
10+
Output: 7
11+
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
12+
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
13+
Total profit is 4 + 3 = 7.
14+
15+
Example 2:
16+
Input: prices = [1,2,3,4,5]
17+
Output: 4
18+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
19+
Total profit is 4.
20+
21+
Example 3:
22+
Input: prices = [7,6,4,3,1]
23+
Output: 0
24+
Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
25+
26+
Constraints:
27+
1 <= prices.length <= 3 * 104
28+
0 <= prices[i] <= 104
29+
*/
30+
class Solution {
31+
static int memo[][] =new int[30001][2];
32+
public int maxProfit(int[] prices) {
33+
//Recursion::)
34+
//return solveRec(prices,0,1);
35+
36+
// Arrays.stream(memo).forEach(a->Arrays.fill(a,-1));
37+
// return solveMemo(prices,0,1);
38+
39+
return solveTab(prices);
40+
41+
}
42+
public static int solveRec(int[] prices ,int indx,int buy){
43+
//Base Case ::
44+
if(indx==prices.length) return 0;
45+
int profit=0;
46+
if(buy==1){
47+
profit = -prices[indx]+solveRec(prices,indx+1,0);
48+
}else{
49+
profit =prices[indx]+solveRec(prices,indx+1,1);
50+
}
51+
int skip =solveRec(prices,indx+1,buy);
52+
return Math.max(profit,skip);
53+
}
54+
public static int solveMemo(int[] prices,int indx,int buy){
55+
//Base Case ::
56+
if(indx==prices.length) return 0;
57+
if(memo[indx][buy]!=-1){
58+
return memo[indx][buy];
59+
}
60+
int profit=0;
61+
if(buy==1){
62+
profit = -prices[indx]+solveMemo(prices,indx+1,0);
63+
}else{
64+
profit =prices[indx]+solveMemo(prices,indx+1,1);
65+
}
66+
int skip =solveMemo(prices,indx+1,buy);
67+
return memo[indx][buy]=Math.max(profit,skip);
68+
}
69+
public static int solveTab(int[] prices){
70+
int n=prices.length;
71+
int tab[][] =new int[n+1][2];
72+
Arrays.stream(tab).forEach(a->Arrays.fill(a,-1));
73+
tab[n][0]=0;
74+
tab[n][1]=0;
75+
76+
int profit=0;
77+
for(int i=n-1;i>=0;i--){
78+
for(int j=0;j<=1;j++){
79+
if(j==1){
80+
profit = Math.max(prices[i]+tab[i+1][0],tab[i+1][1]);
81+
}
82+
if(j==0){
83+
profit = Math.max(-prices[i]+tab[i+1][1],tab[i+1][0]);
84+
}
85+
tab[i][j]=profit;
86+
}
87+
}
88+
return tab[0][0];
89+
}
90+
}

0 commit comments

Comments
 (0)