PHP Max Profit
function maxProfit(array $a): int
{
    $price = $a[0];
    $profit   = 0;
    foreach ($a as $v) {
        $price = min($price, $v);
        $profit   = 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.