
answers가 주어짐.답변 의 등장 횟수를 freq라고 할 때:
groupSize): groupCount): (Math.ceil 이용)function numRabbits(answers: number[]): number {
const groupMap = new Map<number, number>();
// 1. 각 답변별 등장 횟수 카운트
for (const answer of answers) {
groupMap.set(answer, (groupMap.get(answer) ?? 0) + 1);
}
let totalRabbits = 0;
// 2. 답변별 최소 토끼 수 계산 후 합산
for (const [group, freq] of groupMap) {
const groupSize = group + 1; // 그룹 정원
const groupCount = Math.ceil(freq / groupSize); // 필요한 그룹 개수
totalRabbits += groupCount * groupSize;
}
return totalRabbits;
}