
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
โ๏ธ ๋๋ง์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ์ ๋ฆฌํ๊ธฐ (=์ฒด๊ณํ)
https://leetcode.com/problems/two-sum/?envType=study-plan-v2&envId=top-interview-150
Two Sum - LeetCode
Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not
leetcode.com
์ ์ ๋ฐฐ์ด ์ค ๋ ์ซ์์ ํฉ์ด target์ด ๋๋ ๋ ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐํํ๋ผ.
- ์ ํจํ ๋ต์ ํ๋๋ง ์กด์ฌํ๋ค.
- ๋ฆฌ์คํธ ๋ด๋ถ ์์๋ ์๊ด์๋ค.
- | nums | ∈ [2, 10^4]
๐ ์ด๋ค ์์ผ๋ก ์ ๊ทผํ๋๊ฐ?
Two pointer๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์ ๋ ฌ์ ํด์ผ ํ๋ค.
๋ง์ง๋ง์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋ฏธ๋ฆฌ ํํ ํํ๋ก ๊ฐ๊ณผ ์ธ๋ฑ์ค๋ฅผ ํจ๊ป ๋ด์ ๋๋๋ค.
๊ทธ ํ ๋ฐฐ์ด์ ์ ๋ ฌํ๋ค lp์ rp๋ฅผ ์ด์ฉํด ํฉ์ด target์ด ๋๋ ๋ ์์๋ฅผ ์ฐพ๋๋ค.
๐ ์๊ณ ๋ฆฌ์ฆ ์ ํ: ์ ๋ ฅ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ์ผ๋ก
nums์ ๊ธธ์ด๊ฐ ์ต๋ 10^4์ด๊ธฐ ๋๋ฌธ์ 2์ค for๋ฌธ์ผ๋ก ์์ ํ์ํ๋ฉด ์๊ฐ ๋ณต์ก๋ ํจ์จ์ด ๊ต์ฅํ ๋ฎ์์ง๋ค.
Two pointer๋ฅผ ์ด์ฉํ๋ค.
๐ ์์ฌ ์ฝ๋
lst = nums์ (๊ฐ, ์ธ๋ฑ์ค)๋ฅผ ๋ณต์ฌํ ๋ฆฌ์คํธ
lst.sort()
lp, rp = 0, ๋ง์ง๋ง ์ธ๋ฑ์ค
while lp < rp:
if lst[lp][0]์ lst[rp][0]์ ํฉ์ด target๊ณผ ๊ฐ๋ค๋ฉด:
return [lst[lp][1], lst[rp][1]]
elif target ๋ณด๋ค ์๋ค๋ฉด:
lp ํ ์นธ ๋๋ฆฌ๊ธฐ
else:
rp ํ ์นธ ์ค์ด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
lst = [(val, idx) for idx, val in enumerate(nums)]
lst.sort(key=lambda x:x[0])
lp, rp = 0, len(nums) - 1
while lp < rp:
if lst[lp][0] + lst[rp][0] == target:
return [lst[lp][1], lst[rp][1]] # ์๋ ์ธ๋ฑ์ค ๋ฐํ
elif lst[lp][0] + lst[rp][0] < target:
lp += 1
else:
rp -= 1
return []
๋ด ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
lst.sort()์ ์๊ฐ๋ณต์ก๋๋ O(n log n)์ด๋ค. Two pointer๋ฅผ ์ด์ฉํด ๋ ์์ ํฉ์ ๊ณ์ฐํ ๋์ ์๊ฐ๋ณต์ก๋๋ O(n)์ด๋ค.
๋ฐ๋ผ์ ์ ์ฒด ์ฝ๋์ ์๊ฐ ๋ณต์ก๋๋ O(n log n)์ด๋ค.
โ๏ธ ์ฑ๋ฅ ํ๊ณ
1์ด์ ์๊ฐ ์ ํ์ ๊ฐ๋๋ค๊ณ ํ ๋, O(n log n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง ์ฝ๋์์ ์ ๋ ฅ์ ์ต๋ ํฌ๊ธฐ n์ ๋๋ต 10^5์ด๋ค.
๋ฌธ์ ์์ ์ ๋ ฅ์ ์ต๋ ํฌ๊ธฐ๋ 10^4์ด๋ฏ๋ก ์์ ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ค.
๐ก ๋ฌด์์/์ด๋ป๊ฒ ๊ฐ์ ํ ์ ์์๊น?
Hash Table ๋ฌธ์ ์ธ ๋งํผ Hash Table์ ์ด์ฉํ์ฌ ๊ฐ์ ํ ์ ์์ ๊ฒ ๊ฐ๋ค.
Hash Table์ ์ด๋ป๊ฒ ํ์ฉํด์ผํ ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ์ฐ์ Two Pointer๋ก ํ์๋ค.
๋ค๋ฅธ ์ฌ๋์ ์ฝ๋๋ฅผ ํ๊ตฌํด๋ณด๊ธฐ
๐ฏ ๊ตฌํ ์ฝ๋
ํด์ ํ ์ด๋ธ(ํ์ด์ฌ์์๋ ๋์ ๋๋ฆฌ)์ ๊ฐ๊ณผ ์ธ๋ฑ์ค๋ฅผ ๋ด์๋๊ณ ,
nums๋ฅผ ์ํํ๋ฉฐ ๋ณด์(target-nums[i])๋ฅผ ๊ตฌํ๋ค.
๋ณด์๊ฐ ํด์ ํ ์ด๋ธ ์์ ์์ผ๋ฉด์ ์ธ๋ฑ์ค๊ฐ i์ ๋ค๋ฅด๋ค๋ฉด ์ ๋ต์ด๋ค.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dic = dict()
for i, x in enumerate(nums):
dic[x] = i # (key, value): (๊ฐ, ์ธ๋ฑ์ค)
for i in range(len(nums)):
comp = target - nums[i] # complement: ๋ณด์
if comp in dic and dic[comp] != i: # ํด์ ํ
์ด๋ธ์ ๋ณด์๊ฐ ์๊ณ ์ธ๋ฑ์ค๊ฐ ๋ค๋ฅด๋ฉด ์ฐพ์ ๊ฒ
return [i, dic[comp]]
return []
+) ์ฐธ๊ณ ๋ก ๋ณด์๋..

โฑ ์ฑ๋ฅ ์ธก์ (์๊ฐ๋ณต์ก๋/๊ณต๊ฐ๋ณต์ก๋)
O(n)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ๋๋ค.
๋๋ ์
์ ๋ ฌ ์์ด linear search๋ง์ผ๋ก ํจ์จ์ ์ผ๋ก ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ์๋ค.
ํด์ํ ์ด๋ธ์ ์ด๋ฐ์์ผ๋ก ํ์ฉํ ์ ์๋ค๋ ๊ฒ์ด ์๋ก์ ๋ค.
์ฐธ๊ณ ์๋ฃ
Leetcode 1 Two Sum - twopointer sliding window method
'๐ซ ETC > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Week 2-1. [Hash Table] 383. Ransom Note (Python3) (0) | 2023.09.01 |
|---|---|
| Week 2-1. [Hash Table] 219. Contains Duplicate II (Python3) (0) | 2023.08.30 |
| Week 1-2. [Stack] 150. Evaluate Reverse Polish Notation (Python) (0) | 2023.08.29 |
| Week 1-2. [Stack] 155. Min Stack (Python3) (0) | 2023.08.29 |
| Week 1-2. [Linked List] 21. Merge Two Sorted Lists (Python3) (0) | 2023.08.29 |