-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (54 loc) · 1.8 KB
/
main.go
File metadata and controls
61 lines (54 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Source: https://leetcode.com/problems/count-number-of-bad-pairs
// Title: Count Number of Bad Pairs
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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: 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:**
//
// ```
// Input: nums = [1,2,3,4,5]
// Output: 0
// Explanation: There are no bad pairs.
// ```
//
// **Constraints:**
//
// - `1 <= nums.length <= 10^5`
// - `1 <= nums[i] <= 10^9`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
// We find good pair instead.
// IF (i, j) is good, then (nums[i]-i) = (nums[j]-j).
// If (i, j) is good and (j, k) is good, then (i, k) is good.
// That is, (nums[i]-i) = (nums[j]-j) = (nums[k]-k).
// Therefore we can group numbers into equivalence classes.
func countBadPairs(nums []int) int64 {
n := len(nums)
classCount := make(map[int]int64, n)
for i, num := range nums {
classCount[num-i]++
}
badCount := int64(n) * int64(n-1) / 2
for _, size := range classCount {
badCount -= size * (size - 1) / 2
}
return badCount
}