Erlang Max Profit
-module(max_profit).
-export([max_profit/1]).
max_profit([H | T]) ->
{_, Profit} = lists:foldl(fun(V, {MinPrice, MaxProfit}) ->
MinPrice1 = min(MinPrice, V),
{MinPrice1, max(MaxProfit, V - MinPrice1)}
end, {H, 0}, T),
Profit.
This tracks the lowest buy price seen so far and updates the best profit as it scans the prices once.