Best Time to Buy and Sell Stock
public class Solution {
public int MaxProfit(int[] prices) {
if(prices.Length<=1){
return 0;
}
int max_profit=0;
int min_buy_cost=prices[0];
for(int i=1; i<prices.Length;i++){
// sell the stock now
max_profit = Math.Max(max_profit,prices[i]-min_buy_cost);
// buy the stock now
min_buy_cost = Math.Min(min_buy_cost,prices[i]);
}
return max_profit;
}
}Last updated