Skip to content

Commit bef7e46

Browse files
authored
Create count-number-of-pairs-with-absolute-difference-k.py
1 parent 0f9ac13 commit bef7e46

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def countKDifference(self, nums, k):
6+
"""
7+
:type nums: List[int]
8+
:type k: int
9+
:rtype: int
10+
"""
11+
lookup = collections.defaultdict(int)
12+
result = 0
13+
for x in nums:
14+
if x-k in lookup:
15+
result += lookup[x-k]
16+
if x+k in lookup:
17+
result += lookup[x+k]
18+
lookup[x] += 1
19+
return result

0 commit comments

Comments
 (0)