-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest01.java
More file actions
66 lines (61 loc) · 1.75 KB
/
test01.java
File metadata and controls
66 lines (61 loc) · 1.75 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
60
61
62
63
64
65
66
package byteDance;
/**
* Created by lizeyang on 2020/5/12.
* 买卖股票的最佳时机
*/
public class test01 {
//一次买卖
public static int test(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int min = nums[0];
int max = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
} else {
max = Math.max(nums[i] - min, max);
}
}
return max;
}
//两次买卖
public static int test1(int[] nums){
if(nums==null||nums.length<=1){
return 0;
}
int firstBuy = Integer.MIN_VALUE;
int firstSell = 0;
int secondBuy = Integer.MIN_VALUE;
int secondSell = 0;
for(int i=0;i<nums.length;i++){
firstBuy = Math.max(firstBuy,-nums[i]);
firstSell = Math.max(firstSell,nums[i] + firstBuy);
secondBuy = Math.max(secondBuy,firstSell-nums[i]);
secondSell = Math.max(secondSell,nums[i]+secondBuy);
}
return secondSell;
}
//不限次数买卖
public static int test2(int[] nums){
if(nums==null||nums.length<=1){
return 0;
}
int buy = -nums[0];
int sell = 0;
for(int i=1;i<nums.length;i++){
sell = Math.max(sell,buy+nums[i]);
buy = Math.max(buy,sell-nums[i]);
}
return sell;
}
public static void main(String[] args) {
int[] nums = {7, 6, 4, 3, 1};
// System.out.println(test(nums));
int[] nums1 = {1,2,3,4,5};
// System.out.println(test1(nums1));
int[] nums2 = {7,1,5,3,6,4};
System.out.println(test2(nums2));
}
}