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:
parent
92c3e12aad
commit
a82b445053
1 changed files with 11 additions and 0 deletions
11
java/best-time-to-buy-and-sell-stock.java
Normal file
11
java/best-time-to-buy-and-sell-stock.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue