Buy and sell Stock
ayush05
java
7 months ago
442 B
10
Indexable
public static int maxProfit(int prices[]){
int buyPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for(int i=0; i<prices.length; i++){
if(buyPrice < prices[i]){
int profit = prices[i] - buyPrice;
maxProfit = Math.max(maxProfit, profit);
}
else{
buyPrice = prices[i];
}
}
return maxProfit;
}Editor is loading...
Leave a Comment