Leetcode Note: Go - Partition Array Into Three Parts With Equal Sum

Partition Array Into Three Parts With Equal Sum - LeetCode
https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/

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

回答

Go simple hehe - LeetCode Discuss
https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/discuss/2456361/Go-simple-hehe

func canThreePartsEqualSum(arr []int) bool {
    sum := 0
    
    for _, num := range arr {
        sum += num
    }
    
    if sum%3 != 0 {
        return false
    }
    
    partSum, currentSum, count := sum/3, 0, 0
    
    for i := 0; i < len(arr); i++ {
        currentSum += arr[i]
        
        if currentSum == partSum {
            currentSum = 0
            count++
        }
    }

    return count >= 3
}