Skip to content

Commit bc14355

Browse files
committed
feat: add "Contains Duplicate" solution
1 parent e5b0c87 commit bc14355

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
# Constraints
3+
4+
1 <= nums.length <= 10^5
5+
-10^9 <= nums[i] <= 10^9
6+
7+
# Time Complexity: O(n)
8+
9+
๋ฐฐ์—ด ๋‚ด ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์›์†Œ๋ณ„๋กœ ๋“ฑ์žฅํšŸ์ˆ˜๋ฅผ ๊ธฐ๋ก
10+
-> ๋ฌด์Šจ ์›์†Œ๊ฐ€ ๋“ค์–ด์žˆ๋Š”์ง€ ๋ชจ๋ฅด๋ฏ€๋กœ, defaultdict๊ฐ€ ์ข‹์•„๋ณด์ž„
11+
-> ๋“ฑ์žฅ ํšŸ์ˆ˜ ๊ฐ’์ด 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ ๋ฐฐ์—ด ์ˆœํšŒ ๋ฉˆ์ถ”๊ธฐ
12+
13+
# Space Complexity: O(n)
14+
15+
์ตœ๋Œ€ ๋ฐฐ์—ด ์›์†Œ ๊ฐœ์ˆ˜๋งŒํผ key-value ์ง€๋‹ˆ๋Š” ์‚ฌ์ „ ํ™œ์šฉ
16+
"""
17+
18+
from collections import defaultdict
19+
class Solution:
20+
def containsDuplicate(self, nums: List[int]) -> bool:
21+
count_dict = defaultdict(int)
22+
23+
for n in nums:
24+
if count_dict[n] + 1 >= 2 :
25+
return True
26+
27+
else :
28+
count_dict[n] += 1
29+
30+
return False

0 commit comments

Comments
ย (0)