Leetcode 1431) Kids With the Greatest Number of Candies

image image

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> l = new ArrayList<>();
        for(int i =0;i<candies.length;i++){
            if(extraCandies+candies[i]>=max){
                l.add(true);
            }else{
                l.add(false);
            }
        }
        return l;
    }
}

다른 풀이

  • int max에 Integer.MIN_VALUE;를 넣어줌. 그후, Math.max를 사용해서, 캔디의 최댓값을 먼저 구해줌, (나는 if문으로 구했다.)
class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int max = Integer.MIN_VALUE; 
        for(int i = 0; i < candies.length; i++){
            max = Math.max(max, candies[i]);
        }
        List<Boolean> result = new ArrayList<>();
        for(int i = 0; i < candies.length; i++){
            if(candies[i] + extraCandies >= max){
                result.add(true);
            }else{
                result.add(false);
            }
        }
        return result;
    }
}

© 2018. All rights reserved.

Powered by Hydejack v8.5.2