Skip to content

Commit e646ee6

Browse files
authored
feat: add solutions to lc problem: No.0825 (#3767)
1 parent 31646a1 commit e646ee6

File tree

7 files changed

+205
-140
lines changed

7 files changed

+205
-140
lines changed

solution/0800-0899/0825.Friends Of Appropriate Ages/README.md

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### 方法一
80+
### 方法一:计数 + 枚举
81+
82+
我们可以用一个长度为 $121$ 的数组 $\textit{cnt}$ 记录每个年龄的人数。
83+
84+
接下来,枚举所有可能的年龄对 $(\textit{ax}, \textit{ay})$,如果 $\textit{ax}$ 和 $\textit{ay}$ 不满足题目中的任意一个条件,这些年龄对 $(\textit{ax}, \textit{ay})$ 就可以互发好友请求。
85+
86+
此时,如果 $\textit{ax} = \textit{ay}$,年龄相同,那么 $\textit{ax}$ 和 $\textit{ay}$ 之间的好友请求数为 $\textit{cnt}[\textit{ax}] \times (\textit{cnt}[\textit{ax}] - 1)$;否则,年龄不同,那么 $\textit{ax}$ 和 $\textit{ay}$ 之间的好友请求数为 $\textit{cnt}[\textit{ax}] \times \textit{cnt}[\textit{ay}]$。我们将这些好友请求数累加到答案中即可。
87+
88+
时间复杂度 $O(n + m^2)$,其中 $n$ 是数组 $\textit{ages}$ 的长度,而 $m$ 是年龄的最大值,本题中 $m = 121$。
8189

8290
<!-- tabs:start -->
8391

@@ -86,16 +94,14 @@ tags:
8694
```python
8795
class Solution:
8896
def numFriendRequests(self, ages: List[int]) -> int:
89-
counter = Counter(ages)
97+
cnt = [0] * 121
98+
for x in ages:
99+
cnt[x] += 1
90100
ans = 0
91-
for i in range(1, 121):
92-
n1 = counter[i]
93-
for j in range(1, 121):
94-
n2 = counter[j]
95-
if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
96-
ans += n1 * n2
97-
if i == j:
98-
ans -= n2
101+
for ax, x in enumerate(cnt):
102+
for ay, y in enumerate(cnt):
103+
if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)):
104+
ans += x * (y - int(ax == ay))
99105
return ans
100106
```
101107

@@ -104,20 +110,16 @@ class Solution:
104110
```java
105111
class Solution {
106112
public int numFriendRequests(int[] ages) {
107-
int[] counter = new int[121];
108-
for (int age : ages) {
109-
++counter[age];
113+
final int m = 121;
114+
int[] cnt = new int[m];
115+
for (int x : ages) {
116+
++cnt[x];
110117
}
111118
int ans = 0;
112-
for (int i = 1; i < 121; ++i) {
113-
int n1 = counter[i];
114-
for (int j = 1; j < 121; ++j) {
115-
int n2 = counter[j];
116-
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
117-
ans += n1 * n2;
118-
if (i == j) {
119-
ans -= n2;
120-
}
119+
for (int ax = 1; ax < m; ++ax) {
120+
for (int ay = 1; ay < m; ++ay) {
121+
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
122+
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
121123
}
122124
}
123125
}
@@ -132,16 +134,16 @@ class Solution {
132134
class Solution {
133135
public:
134136
int numFriendRequests(vector<int>& ages) {
135-
vector<int> counter(121);
136-
for (int age : ages) ++counter[age];
137+
const int m = 121;
138+
vector<int> cnt(m);
139+
for (int x : ages) {
140+
++cnt[x];
141+
}
137142
int ans = 0;
138-
for (int i = 1; i < 121; ++i) {
139-
int n1 = counter[i];
140-
for (int j = 1; j < 121; ++j) {
141-
int n2 = counter[j];
142-
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
143-
ans += n1 * n2;
144-
if (i == j) ans -= n2;
143+
for (int ax = 1; ax < m; ++ax) {
144+
for (int ay = 1; ay < m; ++ay) {
145+
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
146+
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
145147
}
146148
}
147149
}
@@ -153,25 +155,49 @@ public:
153155
#### Go
154156
155157
```go
156-
func numFriendRequests(ages []int) int {
157-
counter := make([]int, 121)
158-
for _, age := range ages {
159-
counter[age]++
158+
func numFriendRequests(ages []int) (ans int) {
159+
cnt := [121]int{}
160+
for _, x := range ages {
161+
cnt[x]++
160162
}
161-
ans := 0
162-
for i := 1; i < 121; i++ {
163-
n1 := counter[i]
164-
for j := 1; j < 121; j++ {
165-
n2 := counter[j]
166-
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
167-
ans += n1 * n2
168-
if i == j {
169-
ans -= n2
170-
}
163+
for ax, x := range cnt {
164+
for ay, y := range cnt {
165+
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
166+
continue
167+
}
168+
if ax == ay {
169+
ans += x * (x - 1)
170+
} else {
171+
ans += x * y
171172
}
172173
}
173174
}
174-
return ans
175+
176+
return
177+
}
178+
```
179+
180+
#### TypeScript
181+
182+
```ts
183+
function numFriendRequests(ages: number[]): number {
184+
const m = 121;
185+
const cnt = Array(m).fill(0);
186+
for (const x of ages) {
187+
cnt[x]++;
188+
}
189+
190+
let ans = 0;
191+
for (let ax = 0; ax < m; ax++) {
192+
for (let ay = 0; ay < m; ay++) {
193+
if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) {
194+
continue;
195+
}
196+
ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0));
197+
}
198+
}
199+
200+
return ans;
175201
}
176202
```
177203

solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Counting + Enumeration
79+
80+
We can use an array $\textit{cnt}$ of length $121$ to record the number of people of each age.
81+
82+
Next, we enumerate all possible age pairs $(\textit{ax}, \textit{ay})$. If $\textit{ax}$ and $\textit{ay}$ satisfy the conditions given in the problem, these age pairs $(\textit{ax}, \textit{ay})$ can send friend requests to each other.
83+
84+
If $\textit{ax} = \textit{ay}$, meaning the ages are the same, then the number of friend requests between $\textit{ax}$ and $\textit{ay}$ is $\textit{cnt}[\textit{ax}] \times (\textit{cnt}[\textit{ax}] - 1)$. Otherwise, if the ages are different, the number of friend requests between $\textit{ax}$ and $\textit{ay}$ is $\textit{cnt}[\textit{ax}] \times \textit{cnt}[\textit{ay}]$. We accumulate these friend request counts into the answer.
85+
86+
The time complexity is $O(n + m^2)$, where $n$ is the length of the array $\textit{ages}$, and $m$ is the maximum age, which is $121$ in this problem.
7987

8088
<!-- tabs:start -->
8189

@@ -84,16 +92,14 @@ tags:
8492
```python
8593
class Solution:
8694
def numFriendRequests(self, ages: List[int]) -> int:
87-
counter = Counter(ages)
95+
cnt = [0] * 121
96+
for x in ages:
97+
cnt[x] += 1
8898
ans = 0
89-
for i in range(1, 121):
90-
n1 = counter[i]
91-
for j in range(1, 121):
92-
n2 = counter[j]
93-
if not (j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
94-
ans += n1 * n2
95-
if i == j:
96-
ans -= n2
99+
for ax, x in enumerate(cnt):
100+
for ay, y in enumerate(cnt):
101+
if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)):
102+
ans += x * (y - int(ax == ay))
97103
return ans
98104
```
99105

@@ -102,20 +108,16 @@ class Solution:
102108
```java
103109
class Solution {
104110
public int numFriendRequests(int[] ages) {
105-
int[] counter = new int[121];
106-
for (int age : ages) {
107-
++counter[age];
111+
final int m = 121;
112+
int[] cnt = new int[m];
113+
for (int x : ages) {
114+
++cnt[x];
108115
}
109116
int ans = 0;
110-
for (int i = 1; i < 121; ++i) {
111-
int n1 = counter[i];
112-
for (int j = 1; j < 121; ++j) {
113-
int n2 = counter[j];
114-
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
115-
ans += n1 * n2;
116-
if (i == j) {
117-
ans -= n2;
118-
}
117+
for (int ax = 1; ax < m; ++ax) {
118+
for (int ay = 1; ay < m; ++ay) {
119+
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
120+
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
119121
}
120122
}
121123
}
@@ -130,16 +132,16 @@ class Solution {
130132
class Solution {
131133
public:
132134
int numFriendRequests(vector<int>& ages) {
133-
vector<int> counter(121);
134-
for (int age : ages) ++counter[age];
135+
const int m = 121;
136+
vector<int> cnt(m);
137+
for (int x : ages) {
138+
++cnt[x];
139+
}
135140
int ans = 0;
136-
for (int i = 1; i < 121; ++i) {
137-
int n1 = counter[i];
138-
for (int j = 1; j < 121; ++j) {
139-
int n2 = counter[j];
140-
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
141-
ans += n1 * n2;
142-
if (i == j) ans -= n2;
141+
for (int ax = 1; ax < m; ++ax) {
142+
for (int ay = 1; ay < m; ++ay) {
143+
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
144+
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
143145
}
144146
}
145147
}
@@ -151,25 +153,49 @@ public:
151153
#### Go
152154
153155
```go
154-
func numFriendRequests(ages []int) int {
155-
counter := make([]int, 121)
156-
for _, age := range ages {
157-
counter[age]++
156+
func numFriendRequests(ages []int) (ans int) {
157+
cnt := [121]int{}
158+
for _, x := range ages {
159+
cnt[x]++
158160
}
159-
ans := 0
160-
for i := 1; i < 121; i++ {
161-
n1 := counter[i]
162-
for j := 1; j < 121; j++ {
163-
n2 := counter[j]
164-
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
165-
ans += n1 * n2
166-
if i == j {
167-
ans -= n2
168-
}
161+
for ax, x := range cnt {
162+
for ay, y := range cnt {
163+
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
164+
continue
165+
}
166+
if ax == ay {
167+
ans += x * (x - 1)
168+
} else {
169+
ans += x * y
169170
}
170171
}
171172
}
172-
return ans
173+
174+
return
175+
}
176+
```
177+
178+
#### TypeScript
179+
180+
```ts
181+
function numFriendRequests(ages: number[]): number {
182+
const m = 121;
183+
const cnt = Array(m).fill(0);
184+
for (const x of ages) {
185+
cnt[x]++;
186+
}
187+
188+
let ans = 0;
189+
for (let ax = 0; ax < m; ax++) {
190+
for (let ay = 0; ay < m; ay++) {
191+
if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) {
192+
continue;
193+
}
194+
ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0));
195+
}
196+
}
197+
198+
return ans;
173199
}
174200
```
175201

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
class Solution {
22
public:
33
int numFriendRequests(vector<int>& ages) {
4-
vector<int> counter(121);
5-
for (int age : ages) ++counter[age];
4+
const int m = 121;
5+
vector<int> cnt(m);
6+
for (int x : ages) {
7+
++cnt[x];
8+
}
69
int ans = 0;
7-
for (int i = 1; i < 121; ++i) {
8-
int n1 = counter[i];
9-
for (int j = 1; j < 121; ++j) {
10-
int n2 = counter[j];
11-
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
12-
ans += n1 * n2;
13-
if (i == j) ans -= n2;
10+
for (int ax = 1; ax < m; ++ax) {
11+
for (int ay = 1; ay < m; ++ay) {
12+
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
13+
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
1414
}
1515
}
1616
}
1717
return ans;
1818
}
19-
};
19+
};
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
func numFriendRequests(ages []int) int {
2-
counter := make([]int, 121)
3-
for _, age := range ages {
4-
counter[age]++
1+
func numFriendRequests(ages []int) (ans int) {
2+
cnt := [121]int{}
3+
for _, x := range ages {
4+
cnt[x]++
55
}
6-
ans := 0
7-
for i := 1; i < 121; i++ {
8-
n1 := counter[i]
9-
for j := 1; j < 121; j++ {
10-
n2 := counter[j]
11-
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
12-
ans += n1 * n2
13-
if i == j {
14-
ans -= n2
15-
}
6+
for ax, x := range cnt {
7+
for ay, y := range cnt {
8+
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
9+
continue
10+
}
11+
if ax == ay {
12+
ans += x * (x - 1)
13+
} else {
14+
ans += x * y
1615
}
1716
}
1817
}
19-
return ans
20-
}
18+
19+
return
20+
}

0 commit comments

Comments
 (0)