2016. Maximum Difference Between Increasing Elements
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 59% Topics: Array
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def maximumDifference(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result, prefix = 0, float("inf")
for x in nums:
result = max(result, x-prefix)
prefix = min(prefix, x)
return result if result else -1
Solution from kamyu104/LeetCode-Solutions · MIT