Python 🐍 easy, explained with comments || O(n) Time O(1) space || Beats 91%
The brute force solution would be iterating through the array and assuming current element is the minimum and finding the difference between the max element tow
The brute force solution would be iterating through the array and assuming current element is the minimum and finding the difference between the max element towards right the side and current element and then updating the max_difference accordingly. \nTo optimize this approach, I stored the current minimum in a variable and iterated through every element assuming it is the local maxima. The time complexity is therefore reduced to O(n) as each element is iterated only once.\n\n```\nclass Solution:\n    def maximumDifference(self, nums: List[int]) -> int:\n        curr_min = nums[0]\n        ans = 0\n        \n        for i in nums:\n            if i < curr_min:\n                curr_min = i\n                \n            ans = max(ans, i-curr_min)\n            \n        return -1 if ans == 0 else ans \n```
['Array', 'Dynamic Programming', 'Python', 'Python3']