Skip to content

Commit e9fa65e

Browse files
committed
Add solution and test-cases for problem 2364
1 parent d569e0c commit e9fa65e

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2364.Count Number of Bad Pairs][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+
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]`.
5+
6+
Return the total number of **bad pairs** in `nums`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [4,1,3,3]
12+
Output: 5
13+
Explanation: The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
14+
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
15+
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
16+
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
17+
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
18+
There are a total of 5 bad pairs, so we return 5.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Count Number of Bad Pairs
23-
```go
2423
```
25-
24+
Input: nums = [1,2,3,4,5]
25+
Output: 0
26+
Explanation: There are no bad pairs.
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int64 {
4+
l := len(nums)
5+
diff := make(map[int]int64)
6+
diff[0-nums[0]] = 1
7+
8+
var ans, count int64
9+
count = 1
10+
for i := 1; i < l; i++ {
11+
a := i - nums[i]
12+
ans += count - diff[a]
13+
diff[a]++
14+
count++
15+
}
16+
return ans
517
}

leetcode/2301-2400/2364.Count-Number-of-Bad-Pairs/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 1, 3, 3}, 5},
17+
{"TestCase2", []int{1, 2, 3, 4, 5}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)