You can calculate the minimum price you’ve seen so far and the maximum profit for each iteration which is the current price - the previous minimum you’ve seen. Apply that to all stocks and you’re done.

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        min_so_far = float('inf')
        max_profit = 0
        for price in prices:
            min_so_far = min(min_so_far, price)
            max_profit = max(max_profit, price - min_so_far)
        
        return max_profit