成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 575. Distribute Candies

djfml / 2644人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/72317.html

相關(guān)文章

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

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

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

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

    susheng 評(píng)論0 收藏0
  • [Leetcode] Candy 分糖果

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

    張憲坤 評(píng)論0 收藏0
  • [LintCode/LeetCode] Candy

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

    baishancloud 評(píng)論0 收藏0
  • leetcode135. Candy

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

    shmily 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<