Leetcode 121 : Best Time to Buy and Sell Stock
Solution to Best Time to Buy and Sell Stock Problem. Watch this video for more detailed explanation.
class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;
for(int i=0;i<prices.length;i++) {
if(prices[i] < minPrice) {
minPrice = prices[i];
} else {
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}
}
return maxProfit;
}
}