Created
September 22, 2018 02:48
-
-
Save yojkim/71d14c6d26a2d9b5321cf7ebde432142 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func numRabbits(answers []int) int { | |
if len(answers) == 0 { | |
return 0 | |
} | |
cnt := make(map[int]int) | |
sum := 0 | |
for _, n := range answers { | |
cnt[n]++ | |
} | |
for k := range cnt { | |
for cnt[k] > 0 { | |
if k == 0 { | |
sum += cnt[k] | |
break | |
} else { | |
sum = sum + k + 1 | |
cnt[k] = cnt[k] - k - 1 | |
} | |
} | |
} | |
return sum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment