-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path219.py
54 lines (36 loc) · 1.33 KB
/
219.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''219. Contains Duplicate II
Created on 2024-12-19 13:49:32
2024-12-19 14:00:57
@author: MilkTea_shih
'''
#%% Packages
#%% Variable
#%% Functions
class Solution_reference:
# Using `set` to record the possible *number* in the range of `k`,
# space complexity: O(k)
def containsNearbyDuplicate(self, nums: list[int], k: int) -> bool:
seen_number: set[int] = set() #record possible value in range of `k`
for index, number in enumerate(nums):
if index > k:
seen_number.remove(nums[index - k - 1]) #sliding window
if number in seen_number:
return True #exist same number in range of `k`
seen_number.add(number) #update current number
return False
class Solution:
# Using `dictionary` to record the *index* of the latest number,
# space complexity: O(n)
def containsNearbyDuplicate(self, nums: list[int], k: int) -> bool:
seen_table: dict[int, int] = {} #number: index
for index, number in enumerate(nums):
if number in seen_table and index - seen_table[number] <= k:
return True
#update number in `seen_table` to the latest index
seen_table[number] = index
return False
#%% Main Function
#%% Main
if __name__ == '__main__':
pass
#%%