public class Solution {
// Keep track of the minimum value
public int maxProfit(int[] prices) {
if (prices.length == 0) return 0;
int min = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price: prices){
min = Math.min(price, min);
maxProfit = Math.max(maxProfit, price-min);
}
return maxProfit;
}
}
Best Time to Buy and Sell Stock
最新推荐文章于 2022-07-29 20:36:59 发布