[LeetCode][JAVA] 1431. Kids With the Greatest Number of Candies

탱귤생귤·2023년 12월 13일
0

LeetCode

목록 보기
3/16

IMG_0865.jpeg

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
         int MAX=0;
        for (int candy : candies) {
            MAX = Math.max(candy, MAX);
        }

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

        for (int i=0; i<candies.length;i++) {
            if (candies[i]+extraCandies>=MAX) ans.add(i,true);
            else ans.add(i,false);
        }

        return ans;
    }
}

I wanted a faster code, and studied with a code in the discussion tab. That person used stream.

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int max= Arrays.stream(candies).max().getAsInt();
        return Arrays.stream(candies).mapToObj(candy->candy+extraCandies>=max).collect(Collectors.toList());
    }
}

0개의 댓글