반응형
1. 문제
- n명의 학생이 가지고 있는 사탕의 수를 나타내는 배열 candies와 남은 사탕 extraCandies가 주어질 때, 각 학생에게 extraCandies를 준 뒤 학생들 중 가장 많은 사탕의 수이면 true를 담고 아니면 false를 담아 반환하라.
2. 해결
function kidsWithCandies(candies: number[], extraCandies: number): boolean[] {
let max = Number.MIN_SAFE_INTEGER;
const ans = []
for(const candy of candies) max = Math.max(max, candy);
for(const candy of candies) {
if(candy + extraCandies >= max) ans.push(true)
else ans.push(false)
}
return ans;
};
- 학생들이 가지고 있는 가장 많은 사탕의 수를 구한다.
- 각 학생들의 사탕 수 + 나머지 사탕 수를 더한 뒤, 앞서 구한 max값보다 크거나 같으면 true를 담고 아니면 false를 담는다.