-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
40 lines (35 loc) · 1.13 KB
/
test.java
File metadata and controls
40 lines (35 loc) · 1.13 KB
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
import java.util.*;
import java.util.Scanner;
class test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Solution s = new Solution();
Vector v = new Vector<>();
while(sc.hasNextInt()){
v.add(sc.nextInt());
}
int[] n = new int[v.size()];
for(int i=0;i<v.size();++i) n[i] = (int)v.elementAt(i);
System.out.println(s.maxProfit(n));
sc.close();
}
}
class Solution {
public int maxProfit(int[] prices) {
return MaxProfit(prices,prices.length,0,0);
}
public int MaxProfit(int[] prices, int index, int earned,int lastMaxCur){
if(index<0)return 0;
int res1=0,res2=0;
int MaxCur = lastMaxCur;
for(int i=index-1;i>0;--i){
if(prices[i]>prices[i-1]){
MaxCur+=prices[i]-prices[i-1];
res1 = earned + MaxCur + MaxProfit(prices, index-2, earned+MaxCur, 0);
}else{
res2 = earned + MaxCur + MaxProfit(prices, index-1, earned+MaxCur, 0);
}
}
return Math.max(res1, res2);
}
}