1431. Kids With the Greatest Number of Candies 풀이

📖 JAVA

There are n kids with candies.

n명의 아이가 사탕들과 있다.


You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

너에게는 정수형 사탕 배열이 주어지는데,

사탕 배열의 candies[i]는 i번째 아이가 가진 사탕의 수를 의미한다.

그리고 정수 extraCandies는, 너가 가진 여분의 사탕 수를 의미한다.


Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

길이 n의 불리언 배열 result를 리턴하라.

result[i]가 ture인 경우는, i번째 아이에게 여분의 사탕 (extraCandies)을 전부 준뒤에

i번째 아이가 가진 사탕의 갯수가 나머지 애들중에서 제일 큰값인 경우이고

그게 아니면 false다.


Note that multiple kids can have the greatest number of candies.

여러명이 가장 많은 수의 사탕을 가질수 있다는 점을 명심하라.


📖 풀이

자바코드링크

public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
    List<Boolean> result = new ArrayList<>();
    // 최고 캔디 값 구하기
    int greatest = 0;
    for(int candy: candies){
        if(greatest< candy){
            greatest = candy;
        }
    }

    // 캔디 순회
    for(int i=0; i < candies.length; i++){
        if(candies[i] + extraCandies >= greatest){
            result.add(true);
            continue;
        }
        result.add(false);
    }

    return result;
}