Skip to content

Commit 6af0efb

Browse files
committed
contains duplicate & two sum
1 parent 53ce7c7 commit 6af0efb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(n) - Counter(nums)๋Š” n๋ฒˆ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ , cnt.items()๋Š” ์ตœ๋Œ€ n๊ฐœ์˜ (key, value) ์Œ์„ ํฌํ•จํ•˜๋ฏ€๋กœ ์ตœ๋Œ€ n๋ฒˆ ์ˆœํšŒ
5+
# ๊ณต๊ฐ„๋ณต์žก๋„: O(n) - Counter๋Š” ์ž…๋ ฅ ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ ๊ณ ์œ  ์›์†Œ๋ฅผ ํ‚ค๋กœ ์ €์žฅํ•˜๋ฏ€๋กœ ์ตœ์•…์˜ ๊ฒฝ์šฐ n๊ฐœ์˜ ํ‚ค๋ฅผ ์ €์žฅํ•จ
6+
def containsDuplicate(self, nums: List[int]) -> bool:
7+
cnt = Counter(nums)
8+
for _, v in cnt.items():
9+
if v >= 2:
10+
return True
11+
else:
12+
return False

โ€Žtwo-sum/hu6r1s.pyโ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
- Time Complexity
3+
- ๋ฐ”๊นฅ for ๋ฃจํ”„๋Š” n - 1๋ฒˆ, ์•ˆ์ชฝ ๋ฃจํ”„๋Š” ์ตœ๋Œ€ n - i - 1๋ฒˆ ์‹คํ–‰
4+
- ์ „์ฒด์ ์œผ๋กœ O(n^2)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง์ง
5+
6+
- Space Complexity
7+
- ๋ณ„๋„์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ
8+
- ๋ณ€์ˆ˜ i, j ๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ด ์‹œ ์‚ฌ์šฉํ•˜๋Š” [i, j]๋งŒ ์กด์žฌ
9+
- ๋”ฐ๋ผ์„œ O(1)์˜ ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง์ง
10+
"""
11+
12+
class Solution:
13+
def twoSum(self, nums: List[int], target: int) -> List[int]:
14+
for i in range(len(nums)-1):
15+
for j in range(i+1, len(nums)):
16+
if nums[i] + nums[j] == target:
17+
return [i, j]

0 commit comments

Comments
ย (0)