Skip to content

Commit 8567a86

Browse files
committed
Add solution #825
1 parent 3abfd6d commit 8567a86

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@
633633
822|[Card Flipping Game](./0822-card-flipping-game.js)|Medium|
634634
823|[Binary Trees With Factors](./0823-binary-trees-with-factors.js)|Medium|
635635
824|[Goat Latin](./0824-goat-latin.js)|Easy|
636+
825|[Friends Of Appropriate Ages](./0825-friends-of-appropriate-ages.js)|Medium|
636637
827|[Making A Large Island](./0827-making-a-large-island.js)|Hard|
637638
830|[Positions of Large Groups](./0830-positions-of-large-groups.js)|Easy|
638639
831|[Masking Personal Information](./0831-masking-personal-information.js)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 825. Friends Of Appropriate Ages
3+
* https://leetcode.com/problems/friends-of-appropriate-ages/
4+
* Difficulty: Medium
5+
*
6+
* There are n persons on a social media website. You are given an integer array ages where ages[i]
7+
* is the age of the ith person.
8+
*
9+
* A Person x will not send a friend request to a person y (x != y) if any of the following
10+
* conditions is true:
11+
* - age[y] <= 0.5 * age[x] + 7
12+
* - age[y] > age[x]
13+
* - age[y] > 100 && age[x] < 100
14+
*
15+
* Otherwise, x will send a friend request to y.
16+
*
17+
* Note that if x sends a request to y, y will not necessarily send a request to x. Also, a person
18+
* will not send a friend request to themself.
19+
*
20+
* Return the total number of friend requests made.
21+
*/
22+
23+
/**
24+
* @param {number[]} ages
25+
* @return {number}
26+
*/
27+
var numFriendRequests = function(ages) {
28+
const ageCount = new Array(121).fill(0);
29+
30+
for (const age of ages) {
31+
ageCount[age]++;
32+
}
33+
34+
let result = 0;
35+
for (let ageX = 1; ageX <= 120; ageX++) {
36+
if (ageCount[ageX] === 0) continue;
37+
for (let ageY = 1; ageY <= 120; ageY++) {
38+
if (ageCount[ageY] === 0) continue;
39+
if (ageY <= 0.5 * ageX + 7) continue;
40+
if (ageY > ageX) continue;
41+
if (ageY > 100 && ageX < 100) continue;
42+
if (ageX === ageY) {
43+
result += ageCount[ageX] * (ageCount[ageY] - 1);
44+
} else {
45+
result += ageCount[ageX] * ageCount[ageY];
46+
}
47+
}
48+
}
49+
50+
return result;
51+
};

0 commit comments

Comments
 (0)