Problem List

Distribute Candies

June 04, 2025Go array, hash tableeasy

Problem

Performance

Complexity

Go Solution
func distributeCandies(candyType []int) int {
    n := len(candyType) / 2
    m := make(map[int]bool)
    for _, num := range candyType {
        m[num] = true
        if len(m) >= n {
            return n
        }
    }

    return min(len(m), n)
}

func min(a,b int) int {
    if a < b {
        return a
    }
    return b
}

LeetCode Problem Link