Skip to content

Commit 62e9cf1

Browse files
committedDec 27, 2021
feat: update solutions to lc problem: No.0825
No.0825.Friends of Appropriate Ages
1 parent 42a8fa6 commit 62e9cf1

File tree

6 files changed

+69
-123
lines changed

6 files changed

+69
-123
lines changed
 

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

+23-41
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,17 @@
7171
```python
7272
class Solution:
7373
def numFriendRequests(self, ages: List[int]) -> int:
74-
def check(a, b):
75-
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)
76-
77-
res = 0
78-
counter = [0] * 121
79-
for age in ages:
80-
counter[age] += 1
74+
counter = Counter(ages)
75+
ans = 0
8176
for i in range(1, 121):
8277
n1 = counter[i]
8378
for j in range(1, 121):
84-
if check(i, j):
85-
n2 = counter[j]
86-
res += (n1 * n2)
79+
n2 = counter[j]
80+
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
81+
ans += n1 * n2
8782
if i == j:
88-
res -= n2
89-
return res
83+
ans -= n2
84+
return ans
9085
```
9186

9287
### **Java**
@@ -100,25 +95,20 @@ class Solution {
10095
for (int age : ages) {
10196
++counter[age];
10297
}
103-
int res = 0;
98+
int ans = 0;
10499
for (int i = 1; i < 121; ++i) {
105100
int n1 = counter[i];
106101
for (int j = 1; j < 121; ++j) {
107-
if (check(i, j)) {
108-
int n2 = counter[j];
109-
res += (n1 * n2);
102+
int n2 = counter[j];
103+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
104+
ans += n1 * n2;
110105
if (i == j) {
111-
res -= n2;
106+
ans -= n2;
112107
}
113108
}
114-
115109
}
116110
}
117-
return res;
118-
}
119-
120-
private boolean check(int a, int b) {
121-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
111+
return ans;
122112
}
123113
}
124114
```
@@ -131,25 +121,21 @@ public:
131121
int numFriendRequests(vector<int>& ages) {
132122
vector<int> counter(121);
133123
for (int age : ages) ++counter[age];
134-
int res = 0;
124+
int ans = 0;
135125
for (int i = 1; i < 121; ++i)
136126
{
137127
int n1 = counter[i];
138128
for (int j = 1; j < 121; ++j)
139129
{
140130
int n2 = counter[j];
141-
if (check(i, j))
131+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
142132
{
143-
res += (n1 * n2);
144-
if (i == j) res -= n2;
133+
ans += n1 * n2;
134+
if (i == j) ans -= n2;
145135
}
146136
}
147137
}
148-
return res;
149-
}
150-
151-
bool check(int a, int b) {
152-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
138+
return ans;
153139
}
154140
};
155141
```
@@ -162,24 +148,20 @@ func numFriendRequests(ages []int) int {
162148
for _, age := range ages {
163149
counter[age]++
164150
}
165-
res := 0
151+
ans := 0
166152
for i := 1; i < 121; i++ {
167153
n1 := counter[i]
168154
for j := 1; j < 121; j++ {
169155
n2 := counter[j]
170-
if check(i, j) {
171-
res += (n1 * n2)
156+
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
157+
ans += n1 * n2
172158
if i == j {
173-
res -= n2
159+
ans -= n2
174160
}
175161
}
176162
}
177163
}
178-
return res
179-
}
180-
181-
func check(a, b int) bool {
182-
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
164+
return ans
183165
}
184166
```
185167

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

+23-41
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,17 @@
6161
```python
6262
class Solution:
6363
def numFriendRequests(self, ages: List[int]) -> int:
64-
def check(a, b):
65-
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)
66-
67-
res = 0
68-
counter = [0] * 121
69-
for age in ages:
70-
counter[age] += 1
64+
counter = Counter(ages)
65+
ans = 0
7166
for i in range(1, 121):
7267
n1 = counter[i]
7368
for j in range(1, 121):
74-
if check(i, j):
75-
n2 = counter[j]
76-
res += (n1 * n2)
69+
n2 = counter[j]
70+
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
71+
ans += n1 * n2
7772
if i == j:
78-
res -= n2
79-
return res
73+
ans -= n2
74+
return ans
8075
```
8176

8277
### **Java**
@@ -88,25 +83,20 @@ class Solution {
8883
for (int age : ages) {
8984
++counter[age];
9085
}
91-
int res = 0;
86+
int ans = 0;
9287
for (int i = 1; i < 121; ++i) {
9388
int n1 = counter[i];
9489
for (int j = 1; j < 121; ++j) {
95-
if (check(i, j)) {
96-
int n2 = counter[j];
97-
res += (n1 * n2);
90+
int n2 = counter[j];
91+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
92+
ans += n1 * n2;
9893
if (i == j) {
99-
res -= n2;
94+
ans -= n2;
10095
}
10196
}
102-
10397
}
10498
}
105-
return res;
106-
}
107-
108-
private boolean check(int a, int b) {
109-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
99+
return ans;
110100
}
111101
}
112102
```
@@ -119,25 +109,21 @@ public:
119109
int numFriendRequests(vector<int>& ages) {
120110
vector<int> counter(121);
121111
for (int age : ages) ++counter[age];
122-
int res = 0;
112+
int ans = 0;
123113
for (int i = 1; i < 121; ++i)
124114
{
125115
int n1 = counter[i];
126116
for (int j = 1; j < 121; ++j)
127117
{
128118
int n2 = counter[j];
129-
if (check(i, j))
119+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
130120
{
131-
res += (n1 * n2);
132-
if (i == j) res -= n2;
121+
ans += n1 * n2;
122+
if (i == j) ans -= n2;
133123
}
134124
}
135125
}
136-
return res;
137-
}
138-
139-
bool check(int a, int b) {
140-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
126+
return ans;
141127
}
142128
};
143129
```
@@ -150,24 +136,20 @@ func numFriendRequests(ages []int) int {
150136
for _, age := range ages {
151137
counter[age]++
152138
}
153-
res := 0
139+
ans := 0
154140
for i := 1; i < 121; i++ {
155141
n1 := counter[i]
156142
for j := 1; j < 121; j++ {
157143
n2 := counter[j]
158-
if check(i, j) {
159-
res += (n1 * n2)
144+
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
145+
ans += n1 * n2
160146
if i == j {
161-
res -= n2
147+
ans -= n2
162148
}
163149
}
164150
}
165151
}
166-
return res
167-
}
168-
169-
func check(a, b int) bool {
170-
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
152+
return ans
171153
}
172154
```
173155

‎solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@ class Solution {
33
int numFriendRequests(vector<int>& ages) {
44
vector<int> counter(121);
55
for (int age : ages) ++counter[age];
6-
int res = 0;
6+
int ans = 0;
77
for (int i = 1; i < 121; ++i)
88
{
99
int n1 = counter[i];
1010
for (int j = 1; j < 121; ++j)
1111
{
1212
int n2 = counter[j];
13-
if (check(i, j))
13+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
1414
{
15-
res += (n1 * n2);
16-
if (i == j) res -= n2;
15+
ans += n1 * n2;
16+
if (i == j) ans -= n2;
1717
}
1818
}
1919
}
20-
return res;
21-
}
22-
23-
bool check(int a, int b) {
24-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
20+
return ans;
2521
}
2622
};

‎solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@ func numFriendRequests(ages []int) int {
33
for _, age := range ages {
44
counter[age]++
55
}
6-
res := 0
6+
ans := 0
77
for i := 1; i < 121; i++ {
88
n1 := counter[i]
99
for j := 1; j < 121; j++ {
1010
n2 := counter[j]
11-
if check(i, j) {
12-
res += (n1 * n2)
11+
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
12+
ans += n1 * n2
1313
if i == j {
14-
res -= n2
14+
ans -= n2
1515
}
1616
}
1717
}
1818
}
19-
return res
20-
}
21-
22-
func check(a, b int) bool {
23-
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
19+
return ans
2420
}

‎solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@ public int numFriendRequests(int[] ages) {
44
for (int age : ages) {
55
++counter[age];
66
}
7-
int res = 0;
7+
int ans = 0;
88
for (int i = 1; i < 121; ++i) {
99
int n1 = counter[i];
1010
for (int j = 1; j < 121; ++j) {
11-
if (check(i, j)) {
12-
int n2 = counter[j];
13-
res += (n1 * n2);
11+
int n2 = counter[j];
12+
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
13+
ans += n1 * n2;
1414
if (i == j) {
15-
res -= n2;
15+
ans -= n2;
1616
}
1717
}
18-
1918
}
2019
}
21-
return res;
22-
}
23-
24-
private boolean check(int a, int b) {
25-
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
20+
return ans;
2621
}
2722
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
class Solution:
22
def numFriendRequests(self, ages: List[int]) -> int:
3-
def check(a, b):
4-
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)
5-
6-
res = 0
7-
counter = [0] * 121
8-
for age in ages:
9-
counter[age] += 1
3+
counter = Counter(ages)
4+
ans = 0
105
for i in range(1, 121):
116
n1 = counter[i]
127
for j in range(1, 121):
13-
if check(i, j):
14-
n2 = counter[j]
15-
res += (n1 * n2)
8+
n2 = counter[j]
9+
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
10+
ans += n1 * n2
1611
if i == j:
17-
res -= n2
18-
return res
12+
ans -= n2
13+
return ans

0 commit comments

Comments
 (0)