Skip to content

Instantly share code, notes, and snippets.

@yojkim
Created September 22, 2018 02:48
Show Gist options
  • Save yojkim/71d14c6d26a2d9b5321cf7ebde432142 to your computer and use it in GitHub Desktop.
Save yojkim/71d14c6d26a2d9b5321cf7ebde432142 to your computer and use it in GitHub Desktop.
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