[Leetcode] 1431. Kids With the Greatest Number of Candies

HyeRyun·2020년 10월 9일
0

Leetcode

목록 보기
1/1

✔️ 문제

Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.

Constraints:

  • 2 <= candies.length <= 100
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50

😎 소스 코드

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function(candies, extraCandies) {
  let answer = Array(candies.length).fill(true);
  const maxCandy = candies.map(candy => candy + extraCandies);

  for (let i = 0; i < maxCandy.length; i++) {
    for (let j = 0; j < candies.length; j++) {
      if (i === j) {
        j++;
      }
      if (maxCandy[i] < candies[j]){
        answer[i] = false;
        break;
      }
    }
  }  
  
  return answer;
};

✊ 문제를 풀고 나서

문제 자체는 굉장히 쉬웠다. easy라서 그런듯?

코드가 굉장히.. es6가 쓰인 문법도 있고 아닌 곳도 있어서 통일성이 떨어지는 것 같아 아쉽다ㅜㅜ 코딩테스트 공부할 다른 사이트 찾다가 여기서 풀어봤는데 유명한 이유가 있는 듯.. 일단 문제가 엄청 많고 인터페이스가 심플해서 맘에 든다. 리트코드 자주 사용해야겠다!

profile
개발개발

0개의 댓글