详情

    时间复杂度 O(n),空间复杂度 O(n)

    1. func distributeCandies(candyType []int) int {
    2. set := map[int]struct{}{}
    3. for _, t := range candyType {
    4. set[t] = struct{}{}
    5. }
    6. ans := len(candyType) / 2
    7. if len(set) < ans {
    8. ans = len(set)
    9. }
    10. return ans
    11. }

    能分到最大糖果的种类数会是 所有糖果种类数 和 糖果数量一半 两者中最小的那个