LeetCode/java/best-time-to-buy-and-sell-stock.java

11 lines
236 B
Java

class Solution {
public int maxProfit(int[] prices) {
int buy = prices[0], profit = 0;
for (var price : prices) {
buy = Math.min(buy, price);
profit = Math.max(profit, price - buy);
}
return profit;
}
}