Skip to content

Commit 506c749

Browse files
authored
Merge pull request #924 from 0xff-dev/825
Add solution and test-cases for problem 825
2 parents 004a9e6 + 5fbfe0b commit 506c749

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [825.Friends Of Appropriate Ages][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
There are `n` persons on a social media website. You are given an integer array `ages` where `ages[i]` is the age of the i<sup>th</sup> person.
5+
6+
A Person `x` will not send a friend request to a person `y` (`x != y`) if any of the following conditions is true:
7+
8+
- `age[y] <= 0.5 * age[x] + 7`
9+
- `age[y] > age[x]`
10+
- `age[y] > 100 && age[x] < 100`
11+
12+
Otherwise, `x` will send a friend request to `y`.
13+
14+
Note that if `x` sends a request to `y`, `y` will not necessarily send a request to `x`. Also, a person will not send a friend request to themself.
15+
16+
Return the total number of friend requests made.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: ages = [16,16]
22+
Output: 2
23+
Explanation: 2 people friend request each other.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Friends Of Appropriate Ages
23-
```go
2428
```
29+
Input: ages = [16,17,18]
30+
Output: 2
31+
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
32+
```
33+
34+
**Example 3:**
2535

36+
```
37+
Input: ages = [20,30,100,110,120]
38+
Output: 3
39+
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
40+
```
2641

2742
## 结语
2843

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(ages []int) int {
6+
count := make(map[int]int)
7+
distinctAges := make([]int, 0)
8+
for _, n := range ages {
9+
count[n]++
10+
if count[n] == 1 {
11+
distinctAges = append(distinctAges, n)
12+
}
13+
}
14+
sort.Ints(distinctAges)
15+
16+
ans := 0
17+
for i := 1; i < len(distinctAges); i++ {
18+
c := 0
19+
for pre := i - 1; pre >= 0; pre-- {
20+
if distinctAges[pre] <= distinctAges[i]/2+7 {
21+
continue
22+
}
23+
if distinctAges[pre] > 100 && distinctAges[i] < 100 {
24+
continue
25+
}
26+
c += count[distinctAges[pre]]
27+
}
28+
ans += c * count[distinctAges[i]]
29+
}
30+
for n, c := range count {
31+
if c == 1 {
32+
continue
33+
}
34+
if n <= n/2+7 {
35+
continue
36+
}
37+
ans += (c - 1) * c
38+
}
39+
40+
return ans
541
}

leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{16, 16}, 2},
17+
{"TestCase2", []int{16, 17, 18}, 2},
18+
{"TestCase3", []int{20, 30, 100, 110, 120}, 3},
19+
{"TestCase4", []int{54, 23, 102, 90, 40, 74, 112, 74, 76, 21}, 22},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)