Leetcode Note: Go - Maximum Difference Between Increasing Elments

Maximum Difference Between Increasing Elements - LeetCode
https://leetcode.com/problems/maximum-difference-between-increasing-elements/

  • Go 言語で取り組んだメモ

回答

Golang solution - Maximum Difference Between Increasing Elements - LeetCode
https://leetcode.com/problems/maximum-difference-between-increasing-elements/solutions/1487303/golang-solution/

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
}