Leetcode Note: Go - Minimum Value to Get Positive Step by Step Sum

Minimum Value to Get Positive Step by Step Sum - LeetCode
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/

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

回答

Go O(n) solution - Minimum Value to Get Positive Step by Step Sum - LeetCode
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/solutions/2537047/go-o-n-solution/

func minStartValue(nums []int) int {
    prefixSum, result := int(0), int(1)
    
    for _, num := range nums {
       prefixSum += num
        
        result = max(result, 1 - prefixSum)
    }
    
    return result
}

func max(a, b int) int {
    if a > b {
        return a
    }
    
    return b
}