-
Notifications
You must be signed in to change notification settings - Fork 0
/
S122.java
36 lines (33 loc) · 1.05 KB
/
S122.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
/**
* LC#122 Best Time to Buy and Sell Stock II
*/
public class S122 {
public static int maxProfit(int[] prices) {
//暴力计算最大利润组合 时间 O(n2)
// int profit = 0;
// for (int i = 0; i < prices.length; i++) {
// for(int j = i + 1; j < prices.length; j++) {
// if(prices[j] > prices[i]) {
// profit += prices[j] - prices[i];
// break;
// }else{
// break;
// }
// }
// }
// return profit;
//卖出价格高于买入就产生交易,一次遍历 时间 O(n)
int profit = 0;
for(int i = 1; i < prices.length; i++) {
if(prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
public static void main(String[] args) {
int[] stock = {7, 1, 5, 3, 6, 4};
// int[] stock = {1, 2, 3, 4, 5};
System.out.println("maxProfit > " + maxProfit(stock));
}
}