forked from dimpeshpanwar/javabasicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockbuyproblem.java
48 lines (35 loc) · 1.07 KB
/
stockbuyproblem.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
import java.util.*;
public class stockbuyproblem {
static int maxProfit(int price[],
int start, int end)
{
if (end <= start)
return 0;
int profit = 0;
for (int i = start; i < end; i++)
{
for (int j = i + 1; j <= end; j++)
{
if (price[j] > price[i])
{
int curr_profit = price[j] - price[i] +
maxProfit(price,
start, i - 1) +
maxProfit(price,
j + 1, end);
profit = Math.max(profit,
curr_profit);
}
}
}
return profit;
}
public static void main(String[] args)
{
int price[] = {100, 180, 260, 310,
40, 535, 695};
int n = price.length;
System.out.print(maxProfit(
price, 0, n - 1));
}
}