Skip to content

Commit

Permalink
feat: Add two-sum solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
thispath98 committed Dec 24, 2024
1 parent cd70330 commit 3e3a08e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions two-sum/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
Intuition:
기쑴에 ν’€μ—ˆλ˜ 3sum λ¬Έμ œμ™€ μœ μ‚¬ν•˜κ²Œ,
ν•΄μ‹œμ— ν˜„μž¬ μˆ«μžμ™€ λ”ν•΄μ„œ target이 λ˜λŠ” 값을 μ°ΎλŠ”λ‹€.
λ§Œμ•½ 없을 경우, ν•΄μ‹œμ— ν˜„μž¬ κ°’κ³Ό 인덱슀λ₯Ό μ €μž₯ν•œλ‹€.
Time Complexity:
O(N):
ν•΄μ‹œλŠ” μ ‘κ·Όν•˜λŠ” 데에 O(1)이 μ†Œμš”λ˜κ³ ,
총 N번 λ°˜λ³΅ν•΄μ•Ό ν•˜λ―€λ‘œ μ‹œκ°„λ³΅μž‘λ„λŠ” O(N)이닀.
Space Complexity:
O(N):
μ΅œμ•…μ˜ 경우 ν•΄μ‹œμ— N개의 μˆ«μžμ™€ 인덱슀λ₯Ό μ €μž₯ν•΄μ•Ό ν•œλ‹€.
"""
complement_dict = {}
for i, num in enumerate(nums):
if target - num in complement_dict:
return [complement_dict[target - num], i]
else:
complement_dict[num] = i

0 comments on commit 3e3a08e

Please sign in to comment.