-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to leetcode problem: No. 0781. Rabbits in Forest
- Loading branch information
Showing
4 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
public int numRabbits(int[] answers) { | ||
Map<Integer, Integer> counter = new HashMap<>(); | ||
for (int e : answers) { | ||
counter.put(e, counter.getOrDefault(e, 0) + 1); | ||
} | ||
int res = 0; | ||
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) { | ||
int answer = entry.getKey(), count = entry.getValue(); | ||
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1); | ||
} | ||
return res; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Solution: | ||
def numRabbits(self, answers: List[int]) -> int: | ||
counter = collections.Counter() | ||
for e in answers: | ||
counter[e] += 1 | ||
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()]) |