close

題目:

Alice has n candies, where the ith candy is of type candyType[i]. Alice noticed that she started to gain weight, so she visited a doctor.

The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her candies very much, and she wants to eat the maximum number of different types of candies while still following the doctor's advice.

Given the integer array candyType of length n, return the maximum number of different types of candies she can eat if she only eats n / 2 of them.

簡易版本:

吃一半想要最多種,只有一種的話就只有一種,否則最多吃全部的一半

解題想法:

四顆糖果只能吃一半,最多有四種->1/2,或是全都是一種->set

code:

class Solution:
    def distributeCandies(self, candyType: List[int]) -> int:
        return min(len(candyType)//2,len(set(candyType)))

arrow
arrow
    文章標籤
    leetcode python3
    全站熱搜

    低階ㄇㄋ 發表在 痞客邦 留言(0) 人氣()