Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0825
Browse files Browse the repository at this point in the history
No.0825.Friends of Appropriate Ages
  • Loading branch information
yanglbme committed Dec 27, 2021
1 parent 42a8fa6 commit 62e9cf1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 123 deletions.
64 changes: 23 additions & 41 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,17 @@
```python
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
def check(a, b):
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)

res = 0
counter = [0] * 121
for age in ages:
counter[age] += 1
counter = Counter(ages)
ans = 0
for i in range(1, 121):
n1 = counter[i]
for j in range(1, 121):
if check(i, j):
n2 = counter[j]
res += (n1 * n2)
n2 = counter[j]
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
ans += n1 * n2
if i == j:
res -= n2
return res
ans -= n2
return ans
```

### **Java**
Expand All @@ -100,25 +95,20 @@ class Solution {
for (int age : ages) {
++counter[age];
}
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i) {
int n1 = counter[i];
for (int j = 1; j < 121; ++j) {
if (check(i, j)) {
int n2 = counter[j];
res += (n1 * n2);
int n2 = counter[j];
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) {
res -= n2;
ans -= n2;
}
}

}
}
return res;
}

private boolean check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
}
```
Expand All @@ -131,25 +121,21 @@ public:
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i)
{
int n1 = counter[i];
for (int j = 1; j < 121; ++j)
{
int n2 = counter[j];
if (check(i, j))
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
{
res += (n1 * n2);
if (i == j) res -= n2;
ans += n1 * n2;
if (i == j) ans -= n2;
}
}
}
return res;
}

bool check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
};
```
Expand All @@ -162,24 +148,20 @@ func numFriendRequests(ages []int) int {
for _, age := range ages {
counter[age]++
}
res := 0
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if check(i, j) {
res += (n1 * n2)
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
res -= n2
ans -= n2
}
}
}
}
return res
}

func check(a, b int) bool {
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
return ans
}
```

Expand Down
64 changes: 23 additions & 41 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,17 @@
```python
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
def check(a, b):
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)

res = 0
counter = [0] * 121
for age in ages:
counter[age] += 1
counter = Counter(ages)
ans = 0
for i in range(1, 121):
n1 = counter[i]
for j in range(1, 121):
if check(i, j):
n2 = counter[j]
res += (n1 * n2)
n2 = counter[j]
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
ans += n1 * n2
if i == j:
res -= n2
return res
ans -= n2
return ans
```

### **Java**
Expand All @@ -88,25 +83,20 @@ class Solution {
for (int age : ages) {
++counter[age];
}
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i) {
int n1 = counter[i];
for (int j = 1; j < 121; ++j) {
if (check(i, j)) {
int n2 = counter[j];
res += (n1 * n2);
int n2 = counter[j];
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) {
res -= n2;
ans -= n2;
}
}

}
}
return res;
}

private boolean check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
}
```
Expand All @@ -119,25 +109,21 @@ public:
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i)
{
int n1 = counter[i];
for (int j = 1; j < 121; ++j)
{
int n2 = counter[j];
if (check(i, j))
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
{
res += (n1 * n2);
if (i == j) res -= n2;
ans += n1 * n2;
if (i == j) ans -= n2;
}
}
}
return res;
}

bool check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
};
```
Expand All @@ -150,24 +136,20 @@ func numFriendRequests(ages []int) int {
for _, age := range ages {
counter[age]++
}
res := 0
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if check(i, j) {
res += (n1 * n2)
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
res -= n2
ans -= n2
}
}
}
}
return res
}

func check(a, b int) bool {
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
return ans
}
```

Expand Down
14 changes: 5 additions & 9 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ class Solution {
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i)
{
int n1 = counter[i];
for (int j = 1; j < 121; ++j)
{
int n2 = counter[j];
if (check(i, j))
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100)))
{
res += (n1 * n2);
if (i == j) res -= n2;
ans += n1 * n2;
if (i == j) ans -= n2;
}
}
}
return res;
}

bool check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
};
14 changes: 5 additions & 9 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ func numFriendRequests(ages []int) int {
for _, age := range ages {
counter[age]++
}
res := 0
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if check(i, j) {
res += (n1 * n2)
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
res -= n2
ans -= n2
}
}
}
}
return res
}

func check(a, b int) bool {
return (a/2+7 < b) && (a >= b) && (a >= 100 || b <= 100)
return ans
}
17 changes: 6 additions & 11 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@ public int numFriendRequests(int[] ages) {
for (int age : ages) {
++counter[age];
}
int res = 0;
int ans = 0;
for (int i = 1; i < 121; ++i) {
int n1 = counter[i];
for (int j = 1; j < 121; ++j) {
if (check(i, j)) {
int n2 = counter[j];
res += (n1 * n2);
int n2 = counter[j];
if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) {
res -= n2;
ans -= n2;
}
}

}
}
return res;
}

private boolean check(int a, int b) {
return (0.5 * a + 7 < b) && (a >= b) && (a >= 100 || b <= 100);
return ans;
}
}
19 changes: 7 additions & 12 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
def check(a, b):
return (0.5 * a + 7 < b) and (a >= b) and (a >= 100 or b <= 100)

res = 0
counter = [0] * 121
for age in ages:
counter[age] += 1
counter = Counter(ages)
ans = 0
for i in range(1, 121):
n1 = counter[i]
for j in range(1, 121):
if check(i, j):
n2 = counter[j]
res += (n1 * n2)
n2 = counter[j]
if not(j <= 0.5 * i + 7 or j > i or (j > 100 and i < 100)):
ans += n1 * n2
if i == j:
res -= n2
return res
ans -= n2
return ans

0 comments on commit 62e9cf1

Please sign in to comment.