-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path532.k-diff-pairs-in-an-array.go
More file actions
141 lines (130 loc) · 2.72 KB
/
532.k-diff-pairs-in-an-array.go
File metadata and controls
141 lines (130 loc) · 2.72 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import "sort"
/*
* @lc app=leetcode id=532 lang=golang
*
* [532] K-diff Pairs in an Array
*
* https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
*
* algorithms
* Easy (31.64%)
* Likes: 645
* Dislikes: 1357
* Total Accepted: 108.2K
* Total Submissions: 342K
* Testcase Example: '[3,1,4,1,5]\n2'
*
*
* Given an array of integers and an integer k, you need to find the number of
* unique k-diff pairs in the array. Here a k-diff pair is defined as an
* integer pair (i, j), where i and j are both numbers in the array and their
* absolute difference is k.
*
*
*
* Example 1:
*
* Input: [3, 1, 4, 1, 5], k = 2
* Output: 2
* Explanation: There are two 2-diff pairs in the array, (1, 3) and (3,
* 5).Although we have two 1s in the input, we should only return the number of
* unique pairs.
*
*
*
* Example 2:
*
* Input:[1, 2, 3, 4, 5], k = 1
* Output: 4
* Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3,
* 4) and (4, 5).
*
*
*
* Example 3:
*
* Input: [1, 3, 1, 5, 4], k = 0
* Output: 1
* Explanation: There is one 0-diff pair in the array, (1, 1).
*
*
*
* Note:
*
* The pairs (i, j) and (j, i) count as the same pair.
* The length of the array won't exceed 10,000.
* All the integers in the given input belong to the range: [-1e7, 1e7].
*
*
*/
// @lc code=start
func findPairs(nums []int, k int) int {
return findPairs3(nums, k)
}
func findPairs3(nums []int, k int) int {
if k < 0 || len(nums) <= 1 {
return 0
}
sort.Ints(nums)
retVal := 0
for begin, end := 0, 1; end < len(nums); {
if begin >= end || nums[begin]+k > nums[end] {
end++
} else if begin > 0 && nums[begin] == nums[begin-1] || nums[begin]+k < nums[end] {
// begin
// |
// [1, 1, ...., 8, 8]
// |
// end
begin++
} else {
begin++
retVal++
}
}
return retVal
}
// [1,2,3,4,5], -1
func findPairs2(nums []int, k int) int {
if k < 0 || len(nums) <= 1 {
return 0
}
hash := map[int]int{}
for _, v := range nums {
hash[v]++
}
retVal := 0
for hKey, hValue := range hash { // iterator hash
if k == 0 {
if hValue >= 2 {
retVal++
}
} else {
if hash[hKey+k] > 0 {
retVal++
}
}
}
return retVal
}
func findPairs1(nums []int, k int) int {
hash := map[int]int{}
for _, v := range nums {
hash[v]++
}
visited, retVal := map[int]bool{}, 0
for _, v := range nums {
smallVal := v - k
if !visited[v+smallVal] && ((k > 0 && hash[smallVal] > 0) || (k == 0 && hash[smallVal] > 1)) {
retVal++
visited[v+smallVal] = true
}
bigVal := v + k
if !visited[v+bigVal] && ((k > 0 && hash[bigVal] > 0) || (k == 0 && hash[bigVal] > 1)) {
retVal++
visited[v+bigVal] = true
}
}
return retVal
}
// @lc code=end