Best time to buy and sell Stock
unknown
java
2 years ago
354 B
5
Indexable
class Solution {
public int maxProfit(int[] prices) {
int mini = prices[0];
int maxProfit = 0;
for(int i = 1; i< prices.length; i++){
int cost = prices[i] - mini;
maxProfit = Math.max(maxProfit, cost);
mini = Math.min(mini, prices[i]);
}
return maxProfit;
}
}Editor is loading...
Leave a Comment