- Given a 0-indexed integer array
numsof sizen, find the maximum difference betweennums[i]andnums[j](i.e.,nums[j] - nums[i]), such that0 <= i < j < nandnums[i] < nums[j]. - Return the maximum difference. If no such
iandjexists, 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