class Solution {
public int maxProfit(int[] prices) {
int minimum=Integer.MAX_VALUE;
int maxprofit=0;
for(int i=0;i<prices.length;i++){
if(prices[i]<minimum) minimum=prices[i];
else if(prices[i]-minimum>maxprofit) maxprofit=prices[i]-minimum;
}
return maxprofit;
}
}
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
maxProfit = 0
minPurchase = prices[0]
for i in range(1, len(prices)):
maxProfit = max(maxProfit, prices[i] - minPurchase)
minPurchase = min(minPurchase, prices[i])
return maxProfit