Skip to content

Commit fe5c405

Browse files
committed
Add 532
1 parent ec37a17 commit fe5c405

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
| 520 | Detect Capital | Easy | O(N) | O(1) | | | |
8282
| 525 | Contiguous Array | Medium | O(N) | O(N) | Hash Table | Very good problem | |
8383
| 528 | Random Pick with Weight | Medium | O(N), O(logN) | O(N), O(1) | Binary Search, Random | Pythonic | |
84+
| 532 | K-diff Pairs in an Array | Medium | O(N) | O(N) | Array, Two Pointers | | |
8485
| 540 | Single Element in a Sorted Array | Medium | O(logN) | O(1) | Binary Search | | |
8586
| 567 | Permutation in String | Medium | O(S1 + S2) | O(1) | String, Two Pointers, Sliding Window | | |
8687
| 624 | Maximum Distance in Arrays | Easy | O(N) | O(1) | Hash Table, Array | | 🔒 |

inputs/532

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[3,1,4,1,5]
2+
2
3+
[1,2,3,4,5]
4+
1
5+
[1,3,1,5,4]
6+
0
7+
[1,2,4,4,3,3,0,9,2,3]
8+
3
9+
[-1,-2,-3]
10+
1

src/532.k-diff-pairs-in-an-array.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# @lc app=leetcode id=532 lang=python3
3+
#
4+
# [532] K-diff Pairs in an Array
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Array, Two Pointers
9+
class Solution:
10+
# 68 ms, 99.66%. Time and Space: O(N)
11+
def findPairs(self, nums: List[int], k: int) -> int:
12+
C = collections.Counter(nums)
13+
if k == 0:
14+
return sum(val > 1 for val in C.values())
15+
return sum(num + k in C for num in C)
16+
# @lc code=end
17+

0 commit comments

Comments
 (0)