diff --git a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md index ab785c8ca..3c4f6ec8b 100644 --- a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md +++ b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/README.md @@ -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 ith 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. +``` ## 结语 diff --git a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution.go b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution.go index d115ccf5e..4341d953f 100644 --- a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution.go +++ b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution.go @@ -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 } diff --git a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution_test.go b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution_test.go index 14ff50eb4..40bbd1b9d 100644 --- a/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution_test.go +++ b/leetcode/801-900/0825.Friends-Of-Appropriate-Ages/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }