URL: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Signed-off-by: Matej Focko <me@mfocko.xyz>
11 lines
236 B
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;
|
|
}
|
|
}
|