--- title: 781. Rabbits in Forest tags: Greedy description: share source code. --- # 781. Rabbits in Forest ```java= class Solution { public int numRabbits(int[] answers) { int cnt [] = new int [1001]; for(int ans : answers){ cnt [ans]++; } int res = 0; for(int i = 0; i < 1001; i++){ if(cnt[i] != 0){ //int num = cnt[i]/(i + 1) + (cnt[i] % (i + 1) > 0 ? 1 : 0 ); int num = 1 + (( cnt[i] - 1)/(i + 1)); res += (i + 1 )* num; } } return res; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up