781.js
/** ------------------------------------------------------------------------------
*
* 781. Rabbits in Forest
* Topics: Hash Table, Greedy
* https://leetcode.com/problems/rabbits-in-forest/description/?envType=daily-question&envId=2025-04-20
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} answers
* @return {number}
*/
var numRabbits = function (answers) {
const map = new Map()
for (let answer of answers) {
map.set(answer, (map.get(answer) || 0) + 1)
}
let count = 0
for (let [x, n] of map) {
const group = x + 1
const groupCount = Math.ceil(n / group)
count += groupCount * group
}
return count
}
console.log(numRabbits([1, 1, 2]))