Skip to content

Add solution and test-cases for problem 825 #924

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
Sep 9, 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
41 changes: 28 additions & 13 deletions leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
# [825.Friends Of Appropriate Ages][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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.

A Person `x` will not send a friend request to a person `y` (`x != y`) if any of the following conditions is true:

- `age[y] <= 0.5 * age[x] + 7`
- `age[y] > age[x]`
- `age[y] > 100 && age[x] < 100`

Otherwise, `x` will send a friend request to `y`.

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.

Return the total number of friend requests made.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: ages = [16,16]
Output: 2
Explanation: 2 people friend request each other.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Friends Of Appropriate Ages
```go
```
Input: ages = [16,17,18]
Output: 2
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
```

**Example 3:**

```
Input: ages = [20,30,100,110,120]
Output: 3
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
```

## 结语

Expand Down
40 changes: 38 additions & 2 deletions leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(ages []int) int {
count := make(map[int]int)
distinctAges := make([]int, 0)
for _, n := range ages {
count[n]++
if count[n] == 1 {
distinctAges = append(distinctAges, n)
}
}
sort.Ints(distinctAges)

ans := 0
for i := 1; i < len(distinctAges); i++ {
c := 0
for pre := i - 1; pre >= 0; pre-- {
if distinctAges[pre] <= distinctAges[i]/2+7 {
continue
}
if distinctAges[pre] > 100 && distinctAges[i] < 100 {
continue
}
c += count[distinctAges[pre]]
}
ans += c * count[distinctAges[i]]
}
for n, c := range count {
if c == 1 {
continue
}
if n <= n/2+7 {
continue
}
ans += (c - 1) * c
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{16, 16}, 2},
{"TestCase2", []int{16, 17, 18}, 2},
{"TestCase3", []int{20, 30, 100, 110, 120}, 3},
{"TestCase4", []int{54, 23, 102, 90, 40, 74, 112, 74, 76, 21}, 22},
}

// 开始测试
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading