-
Notifications
You must be signed in to change notification settings - Fork 43
/
BestTimeToBuyAndSellStock.java
112 lines (95 loc) · 2.96 KB
/
BestTimeToBuyAndSellStock.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package LeetCodeJava.Array;
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
public class BestTimeToBuyAndSellStock {
// V0
public int maxProfit(int[] prices) {
if (prices.length == 1){
return 0;
}
int profit = 0;
int local_min = Integer.MAX_VALUE;
int local_max = -1;
for(int x : prices){
//System.out.println("x = " + x + ", local_min = " + local_min + ", local_max = " + local_max + ", profit = " + profit);
if (local_min == Integer.MAX_VALUE){
local_min = x;
}else if (local_min > x){
local_min = x;
}else if(x > local_min){
local_max = x;
profit = Math.max(profit, local_max - local_min);
// already "sold", can't reuse local_max, so make it as initial value again
local_max = -1;
}
}
return profit;
}
// V0'
public int maxProfit_0_1(int[] prices) {
if (prices.length == 0){
return 0;
}
int res = 0;
int min = -1;
int max = -1;
for (int i : prices){
int cur = i;
//System.out.println("cur = " + cur);
if (min == -1){
min = cur;
continue;
}
if (min > cur){
min = cur;
continue;
}
if (max == -1){
max = cur;
}
if (cur > max){
max = cur;
}
int tmp = max - min;
//System.out.println("max = " + max + " min = " + min + " tmp = " + tmp);
/** NOTE : need to reset max val after get "revenue", so we don't reuse previous max val */
max = -1;
res = Math.max(tmp, res);
}
return res;
}
// V0''
public int maxProfit_0_2(int[] prices) {
int minVal = (int) Math.pow(10, 4);
int maxVal = 0;
int maxProfit = 0;
//HashSet<Integer> seen = new HashSet<Integer>();
for (int i = 0; i < prices.length; i++){
int cur = prices[i];
//seen.add(cur);
if (cur < minVal){
minVal = cur;
}else{
maxVal = cur;
maxProfit = Math.max( maxVal - minVal, maxProfit);
}
}
return maxProfit > 0 ? maxProfit : 0;
}
// V1
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735493/java-c-best-ever-explanation-could-possible/
public int maxProfit_2(int[] prices) {
int lsf = Integer.MAX_VALUE;
int op = 0;
int pist = 0;
for(int i = 0; i < prices.length; i++){
if(prices[i] < lsf){
lsf = prices[i];
}
pist = prices[i] - lsf;
if(op < pist){
op = pist;
}
}
return op;
}
}