Python O(n) time and O(1) space


#1
class Solution:
    # @param A : tuple of integers
    # @return an integer
    def maxProfit(self, A):
        if len(A) <= 1:
            return 0
            
        answer = -math.inf
        maximum = -math.inf

        for i in range(len(A) - 1, 0, -1):
            if A[i] > maximum:
                maximum = A[i]
            if (maximum - A[i - 1]) > answer:
                answer = (maximum - A[i - 1])
            
        if answer <= 0:
            return 0
            
        return answer