-
Notifications
You must be signed in to change notification settings - Fork 2
/
219.py
48 lines (40 loc) · 1.13 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
# [ LeetCode ] 219. Contains Duplicate II
def soltuion(nums: list[int], k: int) -> bool:
number_with_index: dict[int, int] = {}
for idx, number in enumerate(nums):
if number in number_with_index:
if idx - number_with_index[number] <= k:
return True
else:
number_with_index[number] = idx
else:
number_with_index[number] = idx
return False
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[int] | int] | bool]] = [
{
"input": {
"nums": [1, 2, 3, 1],
"k": 3
},
"output": True
},
{
"input": {
"nums": [1, 0, 1, 1],
"k": 1
},
"output": True
},
{
"input": {
"nums": [1, 2, 3, 1, 2, 3],
"k": 2
},
"output": False
}
]
for case in cases:
assert case["output"] == soltuion(
nums=case["input"]["nums"], k=case["input"]["k"]
)