One minute
Leetcode 2016
2016. Maximum Difference Between Increasing Elements
The idea of this solution is we loop through nums
, and if a number is greater than min
we can check whether to make it max
, otherwise if the number is smaller than min
we can re-make min
.
func maximumDifference(nums []int) int {
min := nums[0]
max := -1
for i := 0; i < len(nums); i++ {
if nums[i] > min && nums[i] - min > max {
max = nums[i] - min
} else if nums[i] < min {
min = nums[i]
}
}
return max
}
Read other posts