71
71
``` python
72
72
class Solution :
73
73
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
81
76
for i in range (1 , 121 ):
82
77
n1 = counter[i]
83
78
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
87
82
if i == j:
88
- res -= n2
89
- return res
83
+ ans -= n2
84
+ return ans
90
85
```
91
86
92
87
### ** Java**
@@ -100,25 +95,20 @@ class Solution {
100
95
for (int age : ages) {
101
96
++ counter[age];
102
97
}
103
- int res = 0 ;
98
+ int ans = 0 ;
104
99
for (int i = 1 ; i < 121 ; ++ i) {
105
100
int n1 = counter[i];
106
101
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;
110
105
if (i == j) {
111
- res -= n2;
106
+ ans -= n2;
112
107
}
113
108
}
114
-
115
109
}
116
110
}
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;
122
112
}
123
113
}
124
114
```
@@ -131,25 +121,21 @@ public:
131
121
int numFriendRequests(vector<int >& ages) {
132
122
vector<int > counter(121);
133
123
for (int age : ages) ++counter[ age] ;
134
- int res = 0;
124
+ int ans = 0;
135
125
for (int i = 1; i < 121; ++i)
136
126
{
137
127
int n1 = counter[ i] ;
138
128
for (int j = 1; j < 121; ++j)
139
129
{
140
130
int n2 = counter[ j] ;
141
- if (check(i, j ))
131
+ if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100) ))
142
132
{
143
- res += ( n1 * n2) ;
144
- if (i == j) res -= n2;
133
+ ans += n1 * n2;
134
+ if (i == j) ans -= n2;
145
135
}
146
136
}
147
137
}
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;
153
139
}
154
140
};
155
141
```
@@ -162,24 +148,20 @@ func numFriendRequests(ages []int) int {
162
148
for _, age := range ages {
163
149
counter[age]++
164
150
}
165
- res := 0
151
+ ans := 0
166
152
for i := 1; i < 121; i++ {
167
153
n1 := counter[i]
168
154
for j := 1; j < 121; j++ {
169
155
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
172
158
if i == j {
173
- res -= n2
159
+ ans -= n2
174
160
}
175
161
}
176
162
}
177
163
}
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
183
165
}
184
166
```
185
167
0 commit comments