Leetcode Note: Go - Count Special Quadruplets

Count Special Quadruplets - LeetCode
https://leetcode.com/problems/count-special-quadruplets/

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

回答

Go non-brute force - Count Special Quadruplets - LeetCode
https://leetcode.com/problems/count-special-quadruplets/solutions/2152537/go-non-brute-force/

func countQuadruplets(nums []int) int {
    res, n := 0, len(nums)
    
    count := map[int]int{(nums[n - 1] - nums[n - 2]): 1}
    
    for b := n - 3; b > 0; b-- {
        for a := b - 1; a >= 0; a-- {
            res += count[nums[a] + nums[b]]
        }
        
        c := b
        
        for d := n - 1; d > c; d-- {
            count[nums[d] - nums[c]]++
        }
    }
    
    return res
}