Data Structures & Algorithms View on GitHub โ
Buy And Sell Crypto
Java
class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
for (int fast = 1, slow = 0; fast < prices.length; fast++) {
int profit = prices[fast] - prices[slow];
maxProfit = Math.max(maxProfit, profit);
if (prices[fast] < prices[slow]) {
slow = fast;
}
}
return maxProfit;
}
}Python
class Solution:
def maxProfit(self, prices: List[int]) -> int:
maxProfitTrade, slow = 0, prices[0]
for price in prices:
profit = price - slow
maxProfitTrade = max(maxProfitTrade, profit)
slow = min(slow, price)
return maxProfitTrade