O(1) Space, O(n) Time solution in Python


#1
class Solution:
    # @param A : tuple of integers
    # @return an integer
    def maxProfit(self, A):
        if len(A) == 0:
            return 0

        minimum = A[0]
        maximum = A[0]
        max_diff = 0
        
        for i in range(len(A)):
            if A[i] < minimum:
                minimum = A[i]
                maximum = A[i]
            elif A[i] > maximum:
                maximum = A[i]
            
            diff = maximum - minimum
            if diff > max_diff:
                max_diff = diff
        
        return max_diff