1
+ """
2
+ Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.
3
+
4
+ A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:
5
+
6
+ 0 <= i, j < nums.length
7
+ i != j
8
+ a <= b
9
+ b - a == k
10
+
11
+
12
+ Example 1:
13
+
14
+ Input: nums = [3,1,4,1,5], k = 2
15
+ Output: 2
16
+ Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
17
+ Although we have two 1s in the input, we should only return the number of unique pairs.
18
+ Example 2:
19
+
20
+ Input: nums = [1,2,3,4,5], k = 1
21
+ Output: 4
22
+ Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
23
+ Example 3:
24
+
25
+ Input: nums = [1,3,1,5,4], k = 0
26
+ Output: 1
27
+ Explanation: There is one 0-diff pair in the array, (1, 1).
28
+ Example 4:
29
+
30
+ Input: nums = [1,2,4,4,3,3,0,9,2,3], k = 3
31
+ Output: 2
32
+ Example 5:
33
+
34
+ Input: nums = [-1,-2,-3], k = 1
35
+ Output: 2
36
+
37
+
38
+ Constraints:
39
+
40
+ 1 <= nums.length <= 104
41
+ -107 <= nums[i] <= 107
42
+ 0 <= k <= 107
43
+ """
44
+
45
+
46
+ class Solution :
47
+ def findPairs (self , nums : List [int ], k : int ) -> int :
48
+ hmap , result = {}, set ()
49
+
50
+ for i , num in enumerate (nums ):
51
+ hmap [num ] = i
52
+
53
+ for j , num in enumerate (nums ):
54
+ if num - k in hmap and (num , num - k ) not in result and hmap [num - k ] != j :
55
+ result .add ((num - k , num ))
56
+ return len (result )
0 commit comments