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

資訊專欄INFORMATION COLUMN

[LeetCode] 575. Distribute Candies

djfml / 2599人閱讀

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());
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72317.html

相關文章

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

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

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

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

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

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

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

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

    baishancloud 評論0 收藏0
  • leetcode135. Candy

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

    shmily 評論0 收藏0

發表評論

0條評論

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