C# Max Profit
static int MaxProfit(int[] a)
{
var price = a[0];
var profit = 0;
foreach (var v in a)
{
price = Math.Min(price, v);
profit = Math.Max(profit, v - price);
}
return profit;
}
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.