diff --git a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md index 14b8afa41..f4820e0e8 100755 --- a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md +++ b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md @@ -1,28 +1,30 @@ # [2364.Count Number of Bad Pairs][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 +You are given a **0-indexed** integer array `nums`. A pair of indices `(i, j)` is a **bad pair** if `i < j` and `j - i != nums[j] - nums[i]`. + +Return the total number of **bad pairs** in `nums`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [4,1,3,3] +Output: 5 +Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4. +The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1. +The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1. +The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2. +The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0. +There are a total of 5 bad pairs, so we return 5. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Count Number of Bad Pairs -```go ``` - +Input: nums = [1,2,3,4,5] +Output: 0 +Explanation: There are no bad pairs. +``` ## 结语 diff --git a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution.go b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution.go index d115ccf5e..8ff353a5d 100644 --- a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution.go +++ b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution.go @@ -1,5 +1,17 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int64 { + l := len(nums) + diff := make(map[int]int64) + diff[0-nums[0]] = 1 + + var ans, count int64 + count = 1 + for i := 1; i < l; i++ { + a := i - nums[i] + ans += count - diff[a] + diff[a]++ + count++ + } + return ans } diff --git a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution_test.go b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution_test.go index 14ff50eb4..359e21646 100644 --- a/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution_test.go +++ b/leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 1, 3, 3}, 5}, + {"TestCase2", []int{1, 2, 3, 4, 5}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }