forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
47 lines (35 loc) · 1019 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// Source : https://leetcode.com/problems/rabbits-in-forest/description/
/// Author : liuyubobobo
/// Time : 2010-02-11
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/// Count
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> count;
for(int ans: answers)
count[ans] ++;
int res = 0;
for(const pair<int, int>& p: count){
int group_num = p.second / (p.first + 1);
if(p.second % (p.first + 1))
group_num ++;
res += group_num * (p.first + 1);
}
return res;
}
};
int main() {
vector<int> answers1 = {1, 1, 2};
cout << Solution().numRabbits(answers1) << endl;
vector<int> answers2 = {10, 10, 10};
cout << Solution().numRabbits(answers2) << endl;
vector<int> answers3 = {};
cout << Solution().numRabbits(answers3) << endl;
return 0;
}