java: add «121. Best Time to Buy and Sell Stock»

URL:	https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-03 22:11:08 +01:00
parent 92c3e12aad
commit a82b445053
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,11 @@
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;
}
}