LeetCode1-Distribute Candies-[Easy]

題目Description:###

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

Example 1:
Input: candies = [1,1,2,2,3,3]
Output: 3
Explanation:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too.
The sister has three different kinds of candies.
Example 2:
Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1].
The sister has two different kinds of candies, the brother has only one kind of candies.
Note:
The length of the given array is in range [2, 10,000], and will be even.
The number in given array is in range [-100,000, 100,000].
—————————————————————————

思路1:####

找出candies[]中有多少種不同的糖果。
當(dāng) 種類(lèi)數(shù)x<=糖果總數(shù)1/2 時(shí)该编,姐姐最多能拿到x種糖果迄本;
當(dāng) 種類(lèi)數(shù)x>糖果總數(shù)
1/2 時(shí),姐姐最多能拿到 糖果總數(shù)*1/2 種糖果课竣;

class Solution(object):
    def distributeCandies(self, candies):

        sister = []
        brother = []
        sister.append(candies[0])
        for x in candies:
            if x not in sister:
                sister.append(x)
            
        if len(sister) == len(candies)/2 | len(sister) < len(candies)/2:
            return len(sister)
        else:
            return len(candies)/2

結(jié)果:Time Limit Exceeded

思路2:####

思路1超時(shí)嘉赎,故在思路1的基礎(chǔ)上優(yōu)化;
不需要計(jì)算出candies的種類(lèi),因?yàn)榻憬阕疃嘀荒苣玫絚andies種類(lèi)的一半于樟,一旦種類(lèi)數(shù)計(jì)算達(dá)到一半就不用再計(jì)算了公条。

class Solution(object):
    def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        sister = []
        sister.append(candies[0])
        for x in candies:
            if x not in sister and len(sister)< len(candies)/2:
                sister.append(x)
                
        return len(sister)

結(jié)果:還是 Time Limit Exceeded


思路3###

參考discuss中[awice]的答案。
其實(shí)就是使用 set()去重
發(fā)現(xiàn)自己根本忘了set()


class Solution(object):
    def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        return min(len(candies)/2, len(set(candies)))

結(jié)果:accepted

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末迂曲,一起剝皮案震驚了整個(gè)濱河市靶橱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌路捧,老刑警劉巖关霸,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異杰扫,居然都是意外死亡队寇,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)涉波,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人炭序,你說(shuō)我怎么就攤上這事啤覆。” “怎么了惭聂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵窗声,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我辜纲,道長(zhǎng)笨觅,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任耕腾,我火速辦了婚禮见剩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘扫俺。我一直安慰自己苍苞,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著羹呵,像睡著了一般骂际。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冈欢,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天歉铝,我揣著相機(jī)與錄音,去河邊找鬼凑耻。 笑死太示,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拳话。 我是一名探鬼主播先匪,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼弃衍!你這毒婦竟也來(lái)了呀非?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤镜盯,失蹤者是張志新(化名)和其女友劉穎岸裙,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體速缆,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡降允,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了艺糜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剧董。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖破停,靈堂內(nèi)的尸體忽然破棺而出翅楼,到底是詐尸還是另有隱情,我是刑警寧澤真慢,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布毅臊,位于F島的核電站,受9級(jí)特大地震影響黑界,放射性物質(zhì)發(fā)生泄漏管嬉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一朗鸠、第九天 我趴在偏房一處隱蔽的房頂上張望蚯撩。 院中可真熱鬧,春花似錦烛占、人聲如沸求厕。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)呀癣。三九已至美浦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間项栏,已是汗流浹背浦辨。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留沼沈,地道東北人流酬。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像列另,于是被迫代替她去往敵國(guó)和親芽腾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容