Skip to content

feat: add solutions to lc problem: No.0825 #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 72 additions & 46 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:计数 + 枚举

我们可以用一个长度为 $121$ 的数组 $\textit{cnt}$ 记录每个年龄的人数。

接下来,枚举所有可能的年龄对 $(\textit{ax}, \textit{ay})$,如果 $\textit{ax}$ 和 $\textit{ay}$ 不满足题目中的任意一个条件,这些年龄对 $(\textit{ax}, \textit{ay})$ 就可以互发好友请求。

此时,如果 $\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}]$。我们将这些好友请求数累加到答案中即可。

时间复杂度 $O(n + m^2)$,其中 $n$ 是数组 $\textit{ages}$ 的长度,而 $m$ 是年龄的最大值,本题中 $m = 121$。

<!-- tabs:start -->

Expand All @@ -86,16 +94,14 @@ tags:
```python
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
counter = Counter(ages)
cnt = [0] * 121
for x in ages:
cnt[x] += 1
ans = 0
for i in range(1, 121):
n1 = counter[i]
for j in range(1, 121):
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:
ans -= n2
for ax, x in enumerate(cnt):
for ay, y in enumerate(cnt):
if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)):
ans += x * (y - int(ax == ay))
return ans
```

Expand All @@ -104,20 +110,16 @@ class Solution:
```java
class Solution {
public int numFriendRequests(int[] ages) {
int[] counter = new int[121];
for (int age : ages) {
++counter[age];
final int m = 121;
int[] cnt = new int[m];
for (int x : ages) {
++cnt[x];
}
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 (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) {
ans -= n2;
}
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
Expand All @@ -132,16 +134,16 @@ class Solution {
class Solution {
public:
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
const int m = 121;
vector<int> cnt(m);
for (int x : ages) {
++cnt[x];
}
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 (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) ans -= n2;
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
Expand All @@ -153,25 +155,49 @@ public:
#### Go

```go
func numFriendRequests(ages []int) int {
counter := make([]int, 121)
for _, age := range ages {
counter[age]++
func numFriendRequests(ages []int) (ans int) {
cnt := [121]int{}
for _, x := range ages {
cnt[x]++
}
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
ans -= n2
}
for ax, x := range cnt {
for ay, y := range cnt {
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
continue
}
if ax == ay {
ans += x * (x - 1)
} else {
ans += x * y
}
}
}
return ans

return
}
```

#### TypeScript

```ts
function numFriendRequests(ages: number[]): number {
const m = 121;
const cnt = Array(m).fill(0);
for (const x of ages) {
cnt[x]++;
}

let ans = 0;
for (let ax = 0; ax < m; ax++) {
for (let ay = 0; ay < m; ay++) {
if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) {
continue;
}
ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0));
}
}

return ans;
}
```

Expand Down
118 changes: 72 additions & 46 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Counting + Enumeration

We can use an array $\textit{cnt}$ of length $121$ to record the number of people of each age.

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.

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.

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.

<!-- tabs:start -->

Expand All @@ -84,16 +92,14 @@ tags:
```python
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
counter = Counter(ages)
cnt = [0] * 121
for x in ages:
cnt[x] += 1
ans = 0
for i in range(1, 121):
n1 = counter[i]
for j in range(1, 121):
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:
ans -= n2
for ax, x in enumerate(cnt):
for ay, y in enumerate(cnt):
if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)):
ans += x * (y - int(ax == ay))
return ans
```

Expand All @@ -102,20 +108,16 @@ class Solution:
```java
class Solution {
public int numFriendRequests(int[] ages) {
int[] counter = new int[121];
for (int age : ages) {
++counter[age];
final int m = 121;
int[] cnt = new int[m];
for (int x : ages) {
++cnt[x];
}
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 (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) {
ans -= n2;
}
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
Expand All @@ -130,16 +132,16 @@ class Solution {
class Solution {
public:
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
const int m = 121;
vector<int> cnt(m);
for (int x : ages) {
++cnt[x];
}
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 (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) ans -= n2;
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
Expand All @@ -151,25 +153,49 @@ public:
#### Go

```go
func numFriendRequests(ages []int) int {
counter := make([]int, 121)
for _, age := range ages {
counter[age]++
func numFriendRequests(ages []int) (ans int) {
cnt := [121]int{}
for _, x := range ages {
cnt[x]++
}
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
ans -= n2
}
for ax, x := range cnt {
for ay, y := range cnt {
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
continue
}
if ax == ay {
ans += x * (x - 1)
} else {
ans += x * y
}
}
}
return ans

return
}
```

#### TypeScript

```ts
function numFriendRequests(ages: number[]): number {
const m = 121;
const cnt = Array(m).fill(0);
for (const x of ages) {
cnt[x]++;
}

let ans = 0;
for (let ax = 0; ax < m; ax++) {
for (let ay = 0; ay < m; ay++) {
if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) {
continue;
}
ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0));
}
}

return ans;
}
```

Expand Down
20 changes: 10 additions & 10 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Solution {
public:
int numFriendRequests(vector<int>& ages) {
vector<int> counter(121);
for (int age : ages) ++counter[age];
const int m = 121;
vector<int> cnt(m);
for (int x : ages) {
++cnt[x];
}
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 (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) {
ans += n1 * n2;
if (i == j) ans -= n2;
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
return ans;
}
};
};
32 changes: 16 additions & 16 deletions solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
func numFriendRequests(ages []int) int {
counter := make([]int, 121)
for _, age := range ages {
counter[age]++
func numFriendRequests(ages []int) (ans int) {
cnt := [121]int{}
for _, x := range ages {
cnt[x]++
}
ans := 0
for i := 1; i < 121; i++ {
n1 := counter[i]
for j := 1; j < 121; j++ {
n2 := counter[j]
if !(j <= i/2+7 || j > i || (j > 100 && i < 100)) {
ans += n1 * n2
if i == j {
ans -= n2
}
for ax, x := range cnt {
for ay, y := range cnt {
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
continue
}
if ax == ay {
ans += x * (x - 1)
} else {
ans += x * y
}
}
}
return ans
}

return
}
Loading
Loading