Leetcode Note: Go - Maximum Score After Splitting a String

Maximum Score After Splitting a String - LeetCode
https://leetcode.com/problems/maximum-score-after-splitting-a-string/

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

回答

Go clean solution - Maximum Score After Splitting a String - LeetCode
https://leetcode.com/problems/maximum-score-after-splitting-a-string/solutions/2537032/go-clean-solution/

func maxScore(s string) int {
    var result, totalOnes, leftZeros, leftOnes int
    
    for _, char := range s {
        if char == '1' {
            totalOnes++
        }
    }
    
    for i := 0; i < len(s)-1; i++ {
        if s[i] == '0' {
            leftZeros++
        } else {
            leftOnes++
        }
        
        result = max(result, leftZeros + totalOnes - leftOnes)
    }
    
    return result
}

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