-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1.java
More file actions
59 lines (56 loc) · 1.64 KB
/
Solution1.java
File metadata and controls
59 lines (56 loc) · 1.64 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package test;
import java.util.Scanner;
public class Solution1 {
/**
* 计算你能获得的最大收益
*
* @param prices Prices[i]即第i天的股价
* @return 整型
*/
public int calculateMax(int[] prices) {
if(prices.length<=1)
return 0;
int res=0;
int temp=0;
//第一种情况只要一个或者不要
for(int i=0;i<prices.length;i++){
int x=prices[i];
for(int y=i;y<prices.length;y++){
if(prices[y]>x){
temp=prices[y]-x;
if(temp>res)
res=temp;
}
}
}
// System.out.println("长度"+prices.length);
//第二种情况两次计算
int temp1=0;
for(int x=0;x<prices.length;x++){
for(int y=x+1;y<prices.length;y++){
if(prices[x]<prices[y]){
temp=prices[y]-prices[x];
for(int z=y+1;z<prices.length;z++){
for(int k=z+1;k<prices.length;k++)
if(prices[z]<prices[k]){
temp1=prices[k]-prices[z];
if(temp+temp1>res)
res=temp+temp1;
}
}
}
}
}
return res;
}
public static void main(String[] args) {
Solution1 s=new Solution1();
Scanner sc=new Scanner(System.in);
String[] st=sc.nextLine().split(",");
int[] i=new int[st.length];
for(int x=0;x<st.length;x++){
i[x]=Integer.parseInt(st[x]);
}
System.out.println(s.calculateMax(i));;
}
}