Skip to content
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
30 changes: 16 additions & 14 deletions leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
16 changes: 14 additions & 2 deletions leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

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

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

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