|
3 | 3 |
|
4 | 4 | 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.
|
5 | 5 | '''
|
| 6 | +# Performance |
6 | 7 | '''
|
7 | 8 | Runtime: 96 ms, faster than 93.53% of Python3 online submissions for Contains Duplicate II.
|
8 | 9 | Memory Usage: 20.5 MB, less than 62.50% of Python3 online submissions for Contains Duplicate II.
|
9 | 10 | '''
|
10 | 11 |
|
11 | | -# Create a hashmap to remember the most recent position of unique values |
12 | | -# If we found duplicate and the range is less than k, then return true |
13 | | -# Else remember that index |
| 12 | +# Algorithm Explained |
| 13 | +''' |
| 14 | +Create a hashmap to remember the most recent position of unique values |
| 15 | +If we found duplicate and the range is less than k, then return true |
| 16 | +Else remember that index |
| 17 | + |
| 18 | +Space: O(n) with n is the number of original array |
| 19 | +Complexity: O(n) |
| 20 | +''' |
14 | 21 | class Solution:
|
15 | 22 | def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
|
16 | 23 | pos = {}
|
|
0 commit comments