Problem Link
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Solution
class Solution {
public int maxProfit(int[] prices) {
int maxPrice = 0;
int minPrice = Integer.MAX_VALUE;
int n = prices.length;
for(int i=0; i<n; i++) {
minPrice = Math.min(minPrice, prices[i]);
int profit = prices[i] - minPrice;
maxPrice = Math.max(maxPrice, profit);
}
return maxPrice;
}
}

Top comments (0)