国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 575. Distribute Candies

djfml / 2593人閱讀

Problem

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].

Solution
class Solution {
    public int distributeCandies(int[] candies) {
        Set set = new HashSet<>(candies.length);
        for (int candy: candies) set.add(candy);
        return Math.min(candies.length/2, set.size());
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/72317.html

相關(guān)文章

  • Leetcode PHP題解--D39 575. Distribute Candies

    摘要:題目鏈接題目分析給定一個偶數(shù)長度的數(shù)組,不同數(shù)字代表不同類型的糖果。這一把糖果需要均分給兩個人。計算最多能拿到多少種糖果。思路最極端的情況,每一個都是不同的糖果。那么可以獲得數(shù)組長度除以種糖果。此時,數(shù)組內(nèi)不同元素的個數(shù)。 575. Distribute Candies 題目鏈接 575. Distribute Candies 題目分析 給定一個偶數(shù)長度的數(shù)組,不同數(shù)字代表不同類型的糖...

    luodongseu 評論0 收藏0
  • leetcode刷題筆記(3)(python)

    摘要:題意給出一串二進(jìn)制數(shù)組,求數(shù)組中最長的連續(xù)的個數(shù)思路遍歷數(shù)組判斷,然后將值添加到長度保存數(shù)組中,取保存數(shù)組最大值。本題要考慮輸入的數(shù)組為的狀況。代碼題意給出一個,從里面獲取兩個數(shù)。 485 Max Consecutive Ones題意:給出一串二進(jìn)制數(shù)組,求數(shù)組中最長的連續(xù)1的個數(shù)思路:遍歷數(shù)組判斷,然后將值添加到長度保存數(shù)組中,取保存數(shù)組最大值。本題要考慮輸入的數(shù)組為[0],[1]的...

    susheng 評論0 收藏0
  • [Leetcode] Candy 分糖果

    摘要:貪心法復(fù)雜度時間空間思路典型的貪心法,如果一個孩子比另一個孩子的分高,我們只多給塊糖。我們可以先從左往右遍歷,確保每個孩子根他左邊的孩子相比,如果分高,則糖要多個,如果分比左邊低,就只給一顆。 Candy There are N children standing in a line. Each child is assigned a rating value. You are gi...

    張憲坤 評論0 收藏0
  • [LintCode/LeetCode] Candy

    摘要:保證高的小朋友拿到的糖果更多,我們建立一個分糖果數(shù)組。首先,分析邊界條件如果沒有小朋友,或者只有一個小朋友,分別對應(yīng)沒有糖果,和有一個糖果。排排坐,吃果果。先往右,再往左。右邊高,多一個。總和加上小朋友總數(shù),就是要準(zhǔn)備糖果的總數(shù)啦。 Problem There are N children standing in a line. Each child is assigned a rat...

    baishancloud 評論0 收藏0
  • leetcode135. Candy

    摘要:題目要求假設(shè)有個孩子站成一排,每個孩子擁有一個評估值。我們可以觀察到,每次最遠(yuǎn)只需要額外分發(fā)到距離當(dāng)前最近的評分最高的那個孩子。因為他的糖果數(shù)量的增加并不會影響到之前孩子。當(dāng)有多個最近評分最高的孩子時,則選擇最后一個。 題目要求 There are N children standing in a line. Each child is assigned a rating value....

    shmily 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<