2024-02-29|閱讀時間 ‧ 約 0 分鐘

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

題目敘述

題目會給定一個陣列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顆之間。


演算法

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

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

所以,只要先找出原本全體小朋友個別擁有糖果的最大值。

分享至
成為作者繼續創作的動力吧!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

作者的相關文章

小松鼠的演算法樂園 的其他內容

你可能也想看

發表回應

成為會員 後即可發表留言
© 2024 vocus All rights reserved.