LeetCode: Best Time to Buy and Sell Stock

LeetCode: Best Time to Buy and Sell Stock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class BestTimeToBuyAndSellStock {
public int maxProfit(int[] prices) {
if (prices.length < 2) {
return 0;
}
int min = prices[0];
int max = 0;
for (int i = 1; i < prices.length; i++) {
if (min > prices[i]) {
min = prices[i];
}
if (max < prices[i] - min) {
max = prices[i] - min;
}
}
return max;
}
}