Skip to content

Commit baa7002

Browse files
authored
Create friends-of-appropriate-ages.cpp
1 parent c263c2f commit baa7002

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

C++/friends-of-appropriate-ages.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time: O(a^2 + n), a is the number of ages,
2+
// n is the number of people
3+
// Space: O(a)
4+
5+
class Solution {
6+
public:
7+
int numFriendRequests(vector<int>& ages) {
8+
unordered_map<int, int> count;
9+
for (const auto &age : ages) {
10+
++count[age];
11+
}
12+
int result = 0;
13+
for (const auto &a: count) {
14+
for (const auto &b: count) {
15+
if (request(a.first, b.first)) {
16+
result += a.second * (b.second - (a.first == b.first ? 1 : 0));
17+
}
18+
}
19+
}
20+
return result;
21+
}
22+
23+
private:
24+
bool request(int a, int b) {
25+
return 0.5 * a + 7 < b && b <= a;
26+
}
27+
};

0 commit comments

Comments
 (0)