-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuySellStocksCase5.java
71 lines (64 loc) · 2.05 KB
/
BuySellStocksCase5.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
/*
Name: Buy And Sell Stocks - Two Transactions Allowed
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/dynamic-programming-and-greedy/buy-sell-stocks-tta-official/ojquestion
Statement: You are given a number n, representing the number of days. You are given n numbers, where ith number represents price of stock on ith day.
You are required to print the maximum profit you can make if you are allowed two transactions at-most. Note - There can be no overlapping transaction.
One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).
*/
import java.util.Scanner;
public class BuySellStocksCase5 {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int [] arr = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = scanner.nextInt();
}
int [] dpLeft = new int[n];
int [] dpRight = new int[n];
int minSoFar = arr[0];
for(int i=1; i<n ; i++)
{
if(minSoFar > arr[i])
{
minSoFar = arr[i];
}
if(arr[i] - minSoFar > dpLeft[i-1])
{
dpLeft[i] = arr[i] - minSoFar;
}
else
{
dpLeft[i] = dpLeft[i-1];
}
}
int maxSoFar = arr[arr.length-1];
for(int i= arr.length-2; i>=0; i--)
{
if(maxSoFar < arr[i])
{
maxSoFar = arr[i];
}
if(maxSoFar - arr[i] > dpRight[i+1])
{
dpRight[i] = maxSoFar - arr[i];
}
else
{
dpRight[i] = dpRight[i+1];
}
}
int profit = 0;
for(int i=0; i<n; i++)
{
if(dpLeft[i] + dpRight[i]> profit)
{
profit = dpLeft[i] + dpRight[i];
}
}
System.out.println(profit);
}
}