You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
429 B

2 years ago
package class15;
// leetcode 121
public class Code01_BestTimeToBuyAndSellStock {
public static int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
// 必须在0时刻卖掉[0] - [0]
int ans = 0;
// arr[0...0]
int min = prices[0];
for (int i = 1; i < prices.length; i++) {
min = Math.min(min, prices[i]);
ans = Math.max(ans, prices[i] - min);
}
return ans;
}
}