- Given a 0-indexed integer arrayÂ
nums of sizeÂn, find the maximum difference betweenÂnums[i] andÂnums[j] (i.e.,Ânums[j] - nums[i]), such thatÂ0 <= i < j < n andÂnums[i] < nums[j]. - Return the maximum difference. If no suchÂ
i andÂj exists, returnÂ-1. - https://leetcode.com/problems/maximum-difference-between-increasing-elements/description/
def maximumDifference(self, nums: List[int]) -> int:
minVal = float('inf')
diff = -1
for n in nums:
if n < minVal:
minVal = min(n,minVal)
else:
currDiff = n - minVal
if currDiff > 0:
diff = max(diff, currDiff)
return difftrick
- track the smallest number, and if the number is STRICTLY greater than the smallest num then we get the difference