LeetCode 1431. Kids With the Greatest Number of Candies

題目描述

Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true] 
Explanation: 
Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids. 
Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. 
Kid 3 has 5 candies and this is already the greatest number of candies among the kids. 
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies. 
Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. 

Example 2:

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false] 
Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.

Example 3:

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]

題目思路

  • 思路一、數(shù)組中存放的是孩子們消災(zāi)手中的糖果數(shù),現(xiàn)有額外的糖果违帆,問(wèn)孩子們現(xiàn)在手中糖果加上一部分額外糖果能否和目前孩子們手中糖果最大數(shù)目持平添吗。
    第一步:找到目前孩子們手中最大糖果數(shù)
    第二步:循環(huán)判斷每個(gè)孩子手中糖果加上額外糖果是否大于最大糖果數(shù)
class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
        vector<bool> res;
        int mc = candies[0];
        // 找到 kids 目前手中的最大 candies
        for(int i = 1; i < candies.size(); ++i)
        {
            if(candies[i] > mc) mc=candies[i];   
        }
        
        // kids 手中已知 greatest candies 保存在 mc 中
        for(int i = 0; i < candies.size(); ++i)
        {
            if(candies[i]+extraCandies >= mc){
                res.push_back(true);
            }
            else{
                res.push_back(false);
            }
        }
        return res;
    }
};

總結(jié)展望

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市陷寝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖怎诫,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異贷痪,居然都是意外死亡幻妓,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)劫拢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)肉津,“玉大人强胰,你說(shuō)我怎么就攤上這事∶蒙常” “怎么了偶洋?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)距糖。 經(jīng)常有香客問(wèn)我玄窝,道長(zhǎng),這世上最難降的妖魔是什么悍引? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任恩脂,我火速辦了婚禮,結(jié)果婚禮上吗铐,老公的妹妹穿的比我還像新娘东亦。我一直安慰自己,他們只是感情好唬渗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布典阵。 她就那樣靜靜地躺著,像睡著了一般镊逝。 火紅的嫁衣襯著肌膚如雪壮啊。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天撑蒜,我揣著相機(jī)與錄音歹啼,去河邊找鬼。 笑死座菠,一個(gè)胖子當(dāng)著我的面吹牛狸眼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浴滴,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼拓萌,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了升略?” 一聲冷哼從身側(cè)響起微王,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎品嚣,沒(méi)想到半個(gè)月后炕倘,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翰撑,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年罩旋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瘸恼,死狀恐怖劣挫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情东帅,我是刑警寧澤压固,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站靠闭,受9級(jí)特大地震影響帐我,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜愧膀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一拦键、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧檩淋,春花似錦芬为、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至日戈,卻和暖如春询张,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浙炼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工份氧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人弯屈。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓蜗帜,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親资厉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钮糖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354