<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)575:分糖果

          共 1596字,需瀏覽 4分鐘

           ·

          2022-04-11 11:53

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?分糖果,我們先來看題面:
          https://leetcode-cn.com/problems/distribute-candies/

          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.

          Alice 有 n 枚糖,其中第 i 枚糖的類型為 candyType[i] 。Alice 注意到她的體重正在增長,所以前去拜訪了一位醫(yī)生。

          醫(yī)生建議 Alice 要少攝入糖分,只吃掉她所有糖的 n / 2 即可(n 是一個偶數(shù))。Alice 非常喜歡這些糖,她想要在遵循醫(yī)生建議的情況下,盡可能吃到最多不同種類的糖。

          給你一個長度為 n 的整數(shù)數(shù)組 candyType ,返回:Alice 在僅吃掉 n / 2 枚糖的情況下,可以吃到糖的 最多 種類數(shù)。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?


          示例 1:

          輸入:candyType = [1,1,2,2,3,3]
          輸出:3
          解釋:Alice 只能吃 6 / 2 = 3 枚糖,由于只有 3 種糖,她可以每種吃一枚。

          示例 2:

          輸入:candyType = [1,1,2,3]
          輸出:2
          解釋:Alice 只能吃 4 / 2 = 2 枚糖,不管她選擇吃的種類是 [1,2]、[1,3] 還是 [2,3],她只能吃到兩種不同類的糖。

          示例 3:

          輸入:candyType = [6,6,6,6]
          輸出:1
          解釋:Alice 只能吃 4 / 2 = 2 枚糖,盡管她能吃 2 枚,但只能吃到 1 種糖。


          解題


          主要思路:直觀的想,就是先統(tǒng)計出總共有多少種糖果,若是糖果種類的數(shù)量小于等于糖果總數(shù)量的一半,則直接返回糖果的種類即可,若是超過了糖果總數(shù)量的一半,則直接返回糖果總數(shù)量的一半即可 .

          class?Solution?{
          public:
          ????int?distributeCandies(vector<int>& candies)?{
          ????????unordered_set<int> st;
          ????????//統(tǒng)計總共的糖果的種類
          ????????for(int& num:candies){
          ????????????if(st.count(num)==0){
          ????????????????st.insert(num);
          ????????????}
          ????????}
          ????????//根據(jù)種類是否超過了總的數(shù)量的一半,來判斷返回值
          ????????return?st.size()<=(candies.size()/2)?st.size():candies.size()/2;
          ????}
          };

          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-560題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)561:數(shù)組拆分 I
          LeetCode刷題實戰(zhàn)562:矩陣中最長的連續(xù)1線段
          LeetCode刷題實戰(zhàn)563:二叉樹的坡度
          LeetCode刷題實戰(zhàn)564:尋找最近的回文數(shù)
          LeetCode刷題實戰(zhàn)565:數(shù)組嵌套
          LeetCode刷題實戰(zhàn)566:重塑矩陣
          LeetCode刷題實戰(zhàn)567:字符串的排列
          LeetCode刷題實戰(zhàn)568:最大休假天數(shù)
          LeetCode刷題實戰(zhàn)569:員工薪水中位數(shù)
          LeetCode刷題實戰(zhàn)570:至少有5名直接下屬的經(jīng)理
          LeetCode刷題實戰(zhàn)571:給定數(shù)字的頻率查詢中位數(shù)
          LeetCode刷題實戰(zhàn)572:另一棵樹的子樹
          LeetCode刷題實戰(zhàn)573:松鼠模擬
          LeetCode刷題實戰(zhàn)574:當選者

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  免费观看日批视频 | 免费看成人靠逼 | 午夜影院无码 | 无码123区 | 欧美经典怡红院肏肥屄淫荡视频在线观看 |