Leetcode 575. Distribute Candies

2022-08-11 15:03:34 浏览数 (1)

文章作者:Tyan 博客:noahsnail.com  |  CSDN  |  简书

1. Description

2. Solution

**解析:**Version 1,使用dict。

  • Version 1
代码语言:javascript复制
class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:
        candies = {}
        for candy in candyType:
            candies[candy]  = candies.get(candy, 0)   1
        return min(len(candies), len(candyType) // 2)
        # return min(len(Counter(candyType)), len(candyType) // 2)

**解析:**Version 2,使用set。

  • Version 2
代码语言:javascript复制
class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:
        candies = set()
        for candy in candyType:
            candies.add(candy)
        return min(len(candies), len(candyType) // 2)
        # return min(len(set(candyType)), len(candyType) // 2)

Reference

  1. https://leetcode.com/problems/distribute-candies/

0 人点赞