Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions solution/0219.Contains Duplicate II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
https://leetcode.com/problems/contains-duplicate-ii/

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
'''
# Performance
'''
Runtime: 96 ms, faster than 93.53% of Python3 online submissions for Contains Duplicate II.
Memory Usage: 20.5 MB, less than 62.50% of Python3 online submissions for Contains Duplicate II.
'''

# Algorithm Explained
'''
Create a hashmap to remember the most recent position of unique values
If we found duplicate and the range is less than k, then return true
Else remember that index

Space: O(n) with n is the number of original array
Complexity: O(n)
'''
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
pos = {}
for idx, element in enumerate(nums):
if element in pos and idx - pos[element] <= k:
return True
pos[element] = idx
return False