擁有最多糖果的小孩 Kids With Greatest Num of Candies_Leetcode 精選75題解析

2024/02/29閱讀時間約 6 分鐘

題目敘述

題目會給定一個陣列candies和一個整數extraCandies作為輸入。

陣列candies代表每一位小朋友手上擁有的糖果總數。

問我們,從頭到尾每一位小朋友,如果多給extraCandies顆糖果給其中某一位小朋友,那位小朋友拿到的糖果數量是不是最多的?假如是,則標記為True,假如不是則標記為False。

輸出以boolean陣列的形式返回答案。

題目的原文敘述


測試範例

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest 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.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.

Example 3:

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

約束條件

Constraints:

  • n == candies.length

糖果陣列和小朋友的數目一樣長,總共有n位小朋友。

  • 2 <= n <= 100

小朋友的數目介於2~100之間。

  • 1 <= candies[i] <= 100

每一位小朋友初始的糖果數量介於1~100顆糖果。

  • 1 <= extraCandies <= 50

額外多給的糖果參數介於1~50顆之間。


演算法

題目要問的核心觀念在於:

額外多給糖果之後,某一位小朋友的糖果數量是不是最多的?

以行動支持創作者!付費即可解鎖
本篇內容共 2628 字、0 則留言,僅發佈於Leetcode 精選75題 上機考面試題 詳解你目前無法檢視以下內容,可能因為尚未登入,或沒有該房間的查看權限。
45會員
288內容數
由有業界實戰經驗的演算法工程師, 手把手教你建立解題的框架, 一步步寫出高效、清晰易懂的解題答案。 著重在讓讀者啟發思考、理解演算法,熟悉常見的演算法模板。 深入淺出地介紹題目背後所使用的演算法意義,融會貫通演算法與資料結構的應用。 在幾個經典的題目融入一道題目的多種解法,或者同一招解不同的題目,擴展廣度,並加深印象。
留言0
查看全部
發表第一個留言支持創作者!