# 1431. Kids With the Greatest Number of Candies ## [題目:](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) 孩子王 每個陣列中的值加上附加的數字後,能不能成為陣列中的最大值。 > 測試資料: > > [2,3,5,1,3] > 3 [4,2,1,1,2] 1 [12,1,12] 10 > > 輸出: [true,true,true,false,true] [true,false,false,false,false] [true,false,true] ## 思路一 兩個一次迴圈解決 ```go= func kidsWithCandies(candies []int, extraCandies int) []bool { var threshold = 0 for _, c := range candies{ if threshold < c{ threshold = c } } var b []bool for _, c := range candies{ if c + extraCandies >= threshold{ b = append(b, true) }else{ b = append(b, false) } } return b } ``` ## 思路二 其實可以減少成只使用一個迴圈就行 ```go= func kidsWithCandies(candies []int, extraCandies int) []bool { var threshold = 0 for _, c := range candies { if threshold < c { threshold = c } } threshold -= extraCandies // now we reduce the threshold with extraCandies var b []bool for _, c := range candies { // compare c to threshold if c >= threshold { b = append(b, true) } else { b = append(b, false) } } return b } ```