【数组水题】1431. 拥有最多糖果的孩子

1431. 拥有最多糖果的孩子

解题思路

  • 首先找出数组的最大值
  • 然后创建一个布尔集合
  • 将每一个数组元素加上extraCandies 最后判断是不是最大值

class Solution {
    
    
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
    
    
        // 我们需要判断一个数组的最大值
        int max = 0;

        for(int i = 0; i < candies.length; i++){
    
    
            if(max <= candies[i]){
    
    
                max = candies[i];
            }
        }

        List<Boolean> result = new ArrayList<>();

        for(int i = 0; i < candies.length; i++){
    
    
            result.add(candies[i] + extraCandies >= max);
        }

        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44653420/article/details/131753917