Buy and sell Stock
ayush05
java
a month ago
442 B
9
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