-
-
Notifications
You must be signed in to change notification settings - Fork 245
[haklee] week 3 #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[haklee] week 3 #380
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""TC: O(n), SC: O(1) | ||
|
||
์์ด๋์ด: | ||
๊ณ๋จ์ k๋ฒ์งธ ์นธ๊น์ง ๋๋ฌํ๋ ๋ฐฉ๋ฒ์ ์๋ฅผ f(k)๋ผ๊ณ ํ์. | ||
f(k)๋ ๋ค์์ ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ํ ๊ฐ์ด๋ค. | ||
- k-2๋ฒ์งธ ์นธ๊น์ง ๊ฐ ๋ค์ ๋ ์นธ ๋. ์ฆ, f(k-2) | ||
- k-1๋ฒ์งธ ์นธ๊น์ง ๊ฐ ๋ค์ ๋ ์นธ ๋. ์ฆ, f(k-1) | ||
์ฆ, f(k) = f(k-2) + f(k-1) | ||
|
||
|
||
SC: | ||
- tabulation ๊ณผ์ ์์ ๊ฐ 2๊ฐ๋ง ๊ณ์ ์ ์งํ๋ค. | ||
- ์ฆ, O(1). | ||
|
||
TC: | ||
- ๋จ์ ๋ง์ ๊ณ์ฐ(O(1))์ O(n)๋ฒ ๋ฐ๋ณตํ๋ค. | ||
- ์ฆ, O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
a, b = 1, 1 | ||
for _ in range(n - 1): | ||
a, b = b, a + b | ||
return b | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""TC: O(m*n), SC: O(n) | ||
|
||
coin ์ข ๋ฅ: m | ||
amount ํฌ๊ธฐ: n | ||
|
||
์์ด๋์ด: | ||
๊ฐ k๋ฅผ ๋ง๋ค๋ ํ์ํ ์ต์ ๋์ ์ ์๋ฅผ f(k)๋ผ๊ณ ํ์. | ||
f(k)๋ ๋ค์์ ๊ฒฝ์ฐ ์ค ์ ์ผ ์์ ๊ฐ์ด๋ค. | ||
- ๋์ c1์ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. f(k-c1) + 1๊ฐ์ ๋์ ํ์. | ||
- ๋์ c2์ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. f(k-c2) + 1๊ฐ์ ๋์ ํ์. | ||
- ... | ||
- ๋์ cm์ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. f(k-cm) + 1๊ฐ์ ๋์ ํ์. | ||
์ฆ, f(k) = min(f(k-c1), f(k-c2), ..., f(k-cm)) + 1 | ||
|
||
์ด๋, n๋ณด๋ค ์์ ๋ชจ๋ i์ ๋ํด์ ํ ๋ฒ f(i)๊ฐ์ ๊ณ์ฐํ ์ผ์ด ์์์ผ๋ฉด ์ด๋ฅผ ์ ์ฅํด๋๊ณ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ผ๋ก | ||
์ ๊ทผํด์ ๋ฌธ์ ๋ฅผ ํ ์ ์๋ค. | ||
|
||
SC: | ||
- n๋ณด๋ค ์์ ๋ชจ๋ i์ ๋ํด f(i)๊ฐ์ ์ ์ฅํด๋๋ ๋ฐฐ์ด ํ์. | ||
- ์ฆ, O(n). | ||
|
||
TC: | ||
- ๊ฐ f(i)๋ง๋ค ์ต์ด ๊ณ์ฐ์ m๊ฐ์ ์์ดํ ์ list์ ๋ฃ๊ณ min๊ฐ์ ์ฐพ๋ ๊ณ์ฐ์ ํ ๋ฒ ํ๋ค. O(m). | ||
- ์ต์ด ๊ณ์ฐ์ด ์๋ ๊ฒฝ์ฐ ๋ฐฐ์ด์ ์ ์ฅ๋ ๊ฐ์ ๊ฐ์ ธ์จ๋ค. O(1). | ||
- ๊ฐ f(i)๋ f(i+c1), f(i+c2), ..., f(i+cm)์ ๊ณ์ฐํ ๋ ํธ์ถ๋๋๋ฐ, ์ฌ๊ธฐ์ O(m) + O(1) + ... + O(1) | ||
๋งํผ์ ์๊ฐ์ด ์์๋๋ฏ๋ก ์ข ํฉํ๋ฉด O(m) + (m+1)*O(1) = O(m) ๋งํผ์ ์๊ฐ์ด ์์๋๋ค. | ||
- ์ด๋ฌํ f(i)๊ฐ์ด ์ด n๊ฐ ์๋ค. ์ฆ, O(m*n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
arr = [None for _ in range(amount + 1)] # None๊ฐ์ ์์ง ๊ณ์ฐ๋์ง ์์๋ค๋ ๋ป. | ||
arr[0] = 0 # ์ด๊ธฐํ | ||
|
||
def dp(target): | ||
if arr[target] is None: # ๋ง์ฝ ์์ง f(target)์ด ๊ณ์ฐ๋์ง ์์๋ค๋ฉด | ||
# ๋ชจ๋ ๋์ ๋ค c์ ๋ํด f(target - c)๋ ๋ค์์ ๊ฒฝ์ฐ๋ค๋ง ์ ํจํ๋ค. | ||
# - target์ด ๋์ c์ ํฌ๊ธฐ ์ด์์ ๋์ด์ผ ํ๋ค. | ||
# - ์์ ๊ณ์ฐํด๋ณธ ๊ฒฐ๊ณผ ์ด ๊ธ์ก target - c๋ฅผ ๊ตฌํ ์ ์๋ ๊ฒฝ์ฐ๋ ๋ฌด์. | ||
# - ๊ตฌํ ์ ์๋ ๊ฒ์ผ๋ก ํ๋ช ๋ ๊ฒฝ์ฐ f(x)์ ๊ฐ์ด -1์ด๋ค. | ||
candidates = [ | ||
v for c in coins if target - c >= 0 and (v := dp(target - c)) >= 0 | ||
] | ||
# candidates์ ์ ํจํ f(target - c)๊ฐ์ด ํ๋๋ ์์ผ๋ฉด f(target)์ -1์ด๋ค. | ||
# ๊ทธ๊ฒ ์๋๋ผ๋ฉด candidates์ ๋ค์ด์๋ ๊ฐ ์ค ์ ์ผ ์ ์ ์์ ๋์ ์ ํ์๋ก ํ๋ | ||
# ๊ฒฝ์ฐ์ 1์ ๋ํ ๊ฐ์ f(target)์ ๋ฃ์ด๋ . | ||
arr[target] = -1 if len(candidates) == 0 else min(candidates) + 1 | ||
return arr[target] | ||
|
||
return dp(amount) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"""TC: O(m^n), SC: O(m^n) | ||
|
||
candidates์ ์๋ ๊ฐ์ ๊ฐ์: m | ||
target์ ํฌ๊ธฐ: n | ||
|
||
์์ด๋์ด: | ||
candidates(์ดํ cands)์ ์๋ ์ซ์๋ค์ ๋ํด์ k๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ f(k)๋ผ๊ณ ํ์. | ||
f(k)๋ ๋ค์์ ๊ฒฝ์ฐ๋ค์ ์ข ํฉํ ๊ฒ์ด๋ค. | ||
- cands์ ์๋ c1์ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. ์ฆ, f(k-c1)์ ์๋ ๋ชจ๋ ๋ฐฉ๋ฒ์ ๋์ c1์ ๋ํจ. | ||
- cands์ ์๋ c2๋ฅผ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. ์ฆ, f(k-c2)์ ์๋ ๋ชจ๋ ๋ฐฉ๋ฒ์ ๋์ c2์ ๋ํจ. | ||
... | ||
- cands์ ์๋ cm์ ๋ง์ง๋ง์ผ๋ก ๋ํด์ k๋ฅผ ๋ง๋ค์๋ค. ์ฆ, f(k-cm)์ ์๋ ๋ชจ๋ ๋ฐฉ๋ฒ์ ๋์ cm์ ๋ํจ. | ||
|
||
์ด๋ ๊ฒ ํ๋ฉด ๋ฌธ์ ๋, ํ๋์ ๊ฐ์ ๋ง๋๋ ๋ฐ์ ์ค๋ณต๋ ๊ฒฝ์ฐ๊ฐ ๋์ฌ ์ ์๋ค๋ ๊ฒ์ด๋ค. | ||
e.g.) candidates = [2, 3], target = 5 | ||
f(2) = [[2]] | ||
f(3) = [[3]] | ||
์์ ๊ฐ์ ํ์ฉํด์ f(5)๋ฅผ ๊ตฌํ๋ฉด | ||
f(5) = [f(2)์ ๋ฐฉ๋ฒ๋ค์ ๋์ 3์ ๋ถ์] + [f(3)์ ๋ฐฉ๋ฒ๋ค์ ๋์ 2๋ฅผ ๋ถ์] | ||
= [[2, 3]] + [[3, 2]] | ||
= [[2, 3], [3, 2]] | ||
|
||
๊ทธ๋์ ๋ง์ง๋ง์ ๊ฐ์ ์์ดํ ์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฆฌ์คํธ๋ฅผ ์ฐพ์์ ์ค๋ณต์ ์ ๊ฑฐํด์ค๋ค. | ||
|
||
์ด๋ฒ ๋ฌธ์ ์์๋ [2, 2, 3], [2, 3, 2], [3, 2, 2] ๊ฐ์ด ๋ค์ด๊ฐ๋ ์์ดํ ์ ์์๋ง ๋ค๋ฅธ ๊ฒฝ์ฐ๋ฅผ ๊ฐ์ ๊ฒ์ผ๋ก | ||
๋ณด์๊ธฐ ๋๋ฌธ์ ๋ง์ง๋ง์ ์ค๋ณต์ ์ ๊ฑฐํ์ง๋ง, ๋ง์ฝ ์ด๋ค์ ์๋ก ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ๋ณด๋ ๋ฌธ์ ๊ฐ ์ฃผ์ด์ง๋ค๋ฉด ์์ | ||
๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก ๋ฆฌํดํ๋ฉด ๋๋ค. | ||
|
||
|
||
SC: | ||
- ๋ฌธ์ ํน์ฑ์ f(i)์ ๋ค์ด๊ฐ ์ ์๋ ๋ฐฉ๋ฒ์ ์๋ | ||
- i๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๊ธธ์ด๋ O(i). | ||
- cands์ 1์ด ์๊ณ ์ด 1๋ก ๊ฐ๋ ์ฑ์ด ๋ฐฉ๋ฒ [1, 1, ..., 1]์ ์๊ฐํ๋ฉด ํธํ๋ค. | ||
- cands์ ์ต์๊ฐ์ด ์ด๋ค ์์ x๋ผ๊ณ ํด๋ [x, x, ..., x]์๋ i/x๊ฐ ๋ค์ด๊ฐ๋๋ฐ, O(i/x)๋ O(i). | ||
- ๊ฐ ๋ฐฉ๋ฒ์ ๋ค์ด์๋ ์์ดํ ์ m๊ฐ์ง ๊ฒฝ์ฐ์ ์๊ฐ ๊ฐ๋ฅ. [(c1, c2, ..., cm ์ค ํ๋), ..., (c1, c2, ..., cm ์ค ํ๋)] | ||
- ์ฆ, f(i)์๋ O(m^i)๊ฐ์ง ๊ฒฝ์ฐ๊ฐ ๋ค์ด๊ฐ ์ ์๋ค. | ||
- f(1), f(2), ..., f(n)์ ๋ค ๋ํ๋ฉด O(m^1) + O(m^2) + ... + O(m^n) = O(m^n)์ด ๋๋ค. | ||
- ์ฆ, O(m^n). | ||
|
||
TC: | ||
- ์์ SC์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. O(m^n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
dp = [[] for _ in range(target + 1)] # ์ด๊ธฐํ. | ||
dp[0] = [[]] # 0์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์๋ฌด ์ซ์๋ ๋ฃ์ง ์๋ ๊ฒ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค. | ||
for cur in range(1, target + 1): # f(i)๋ฅผ 1๋ถํฐ ๊ณ์ฐํด๋๊ฐ๋ฉด์ ์ฑ์ฐ๊ธฐ ์์. | ||
for cand in candidates: | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prev = cur - cand | ||
if prev >= 0: | ||
dp[cur] += [combi + [cand] for combi in dp[prev]] | ||
|
||
# ๋ง์ง๋ง์ ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํด์ค๋ค. | ||
return list(set([tuple(sorted(i)) for i in dp[target]])) | ||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dp[target]์ ์ต๋ ๊ธธ์ด๋ target์ ์ต๋ ํฌ๊ธฐ / candidates์ ์ต์ ๊ฐ์ด๋ฏ๋ก 40 / 2 = 20 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋์ํฉ๋๋ค. ๋ง์ฝ ํ์ค์์ ์ด ๋ฌธ์ ๋ฅผ ์ข ๋ ๋นก์ผ ์กฐ๊ฑด์ด ๊ฑธ๋ ค์๋ ์ํ๋ก ๋ง์ฃผ์ณค๋ค๋ฉด ์๋ง ์ด ํ์ด๋ก๋ ๋ถ๋ถ์ ์๋ฐ์ ๋ชป ๊ธ์ด๊ฐ์ ๊ฒ์ ๋๋ค. ๊ทธ๋์ ์ข ๋ ์๊ฐ์ ๋จ์ถํ๋ ๋ฐฉ๋ฒ์ ์ฐพ๋ค๊ฐ ์๋์ ๋ค๋ฅธ ์๋ฃจ์ ๋ค์ ๊ตฌํํ์ง ์์์๊น ์๊ฐํ์ต๋๋ค. |
||
|
||
|
||
""" | ||
์์ด๋์ด: | ||
์ค๊ฐ์ค๊ฐ ๊ณ์ฐํ๋ฉด์ ์ค๋ณต๋ ๊ฐ์ ์ ๊ฑฐํ๋ฉด์ f(i)๊ฐ์ ๊ด๋ฆฌํ๋ ์์ผ๋ก ์ปคํ ํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค. | ||
๊ฐ ๋ฐฉ๋ฒ์ [c1, ...c1, c2, ..., c2, ... , cm, ..., m] ๊ผด์ด ๋๋๋ฐ, | ||
[ ^ ^ ... ^ ] | ||
๊ฐ f(i)๋ง๋ค ์์ `^` ํ์๋ฅผ ํด๋ ๊ณณ์ ์ฐพ๋ ๋ฐฉ๋ฒ์ ์๋งํผ ๊ณต๊ฐ์ด ํ์ํ๋ค. | ||
์ด ์ซ์๋ ๋๋ต (i choose m-1)์ด๋ผ๊ณ ์๊ฐํ ์ ์๋ค. | ||
|
||
๊ทธ๋ฌ๋ฏ๋ก SC์ TC ๋ชจ๋ O(n choose m) = O((n/m)^m)...? | ||
(ref: https://en.wikipedia.org/wiki/Binomial_coefficient#Bounds_and_asymptotic_formulas) | ||
""" | ||
|
||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
dp = [[] for _ in range(target + 1)] | ||
dp[0] = [[]] | ||
for cur in range(1, target + 1): | ||
for cand in candidates: | ||
prev = cur - cand | ||
if prev >= 0: | ||
dp[cur] += [combi + [cand] for combi in dp[prev]] | ||
dp[cur] = list(set([tuple(sorted(i)) for i in dp[cur]])) | ||
return list(set([tuple(sorted(i)) for i in dp[target]])) | ||
|
||
|
||
""" | ||
์์ด๋์ด: | ||
๋ง๋ค๊ณ ๋์ ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํ์ง ๋ง๊ณ , ์ฒ์๋ถํฐ ์ค๋ณต๋ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ง ์๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ด๋ค. | ||
๊ฐ ๋ฐฉ๋ฒ๋ง๋ค ํด๋น ๋ฐฉ๋ฒ์์ ์ฌ์ฉํ ์ต๋ candidate ์ธ๋ฑ์ค๋ฅผ ๋ฌ์๋๊ณ , ์ดํ ํด๋ฅผ ๊ตฌํ ๋๋ ํด๋น | ||
์ธ๋ฑ์ค ์ด์์ candidate๋ง ์ถ๊ฐํ ์ ์๋๋ก ๋จ์๋ฅผ ๋ฌ์๋๋ ๋ฐฉ์์ผ๋ก ๊ตฌํ ๊ฐ๋ฅํ๋ค. | ||
e.g.) candidate = [2, 5, 3] | ||
f(10)์ ๋ค์ด์์ ์ ์๋ ๋ฐฉ๋ฒ์ | ||
- ([2, 2, 2, 2, 2], 0) : ๋ง์ง๋ง ์์ดํ ์ดํ์ 2, 5, 3 ์ ๋ถ ๋ฑ์ฅ ๊ฐ๋ฅ. | ||
- ([5, 5], 1) : ๋ง์ง๋ง ์์ดํ ์ดํ์ 5, 3๋ง ๋ฑ์ฅ ๊ฐ๋ฅ. | ||
- ([2, 5, 3], 2) : ๋ง์ง๋ง ์์ดํ ์ดํ์ 3๋ง ๋ฑ์ฅ ๊ฐ๋ฅ. | ||
- ([2, 2, 3, 3], 2) : ๋ง์ง๋ง ์์ดํ ์ดํ์ 3๋ง ๋ฑ์ฅ ๊ฐ๋ฅ. | ||
|
||
SC์ TC๋ ๋ฐ๋ก ์์์ O(n choose m)์ ๊ณ์ฐํ ๊ฒ๊ณผ ๊ฐ์ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค. | ||
""" | ||
|
||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
dp = [[] for _ in range(target + 1)] | ||
dp[0] = [([], 0)] # (๋ฐฉ๋ฒ, ์ฌ์ฉ ๊ฐ๋ฅํ candidate index ์ต์๊ฐ) ์. | ||
for cur in range(1, target + 1): | ||
for i in range(len(candidates)): | ||
prev = cur - candidates[i] | ||
if prev >= 0: | ||
dp[cur] += [ | ||
(combi[0] + [candidates[i]], i) | ||
for combi in dp[prev] | ||
if i >= combi[1] | ||
] | ||
return [i[0] for i in dp[target]] # ๋ฐฉ๋ฒ๋ง ์ถ์ถํด์ ๋ฆฌํดํ๋ค. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""TC: O(n), SC: O(n) | ||
|
||
์์ด๋์ด: | ||
๋ค์์ ์ธ ์ํฉ ์ค ํ๋๊ฐ ๋ ๊ฒ์ด๋ค. | ||
- 0์ด ๋ ์ด์ ํฌํจ. ๋ชจ๋ ๊ฒฐ๊ณผ๊ฐ์ด 0์ด ๋๋ค. | ||
- 0์ด ํ๋ ํฌํจ. | ||
- 0์ ํด๋นํ๋ ์ธ๋ฑ์ค์๋ nums์ ์๋ ์ซ์๋ค ์ค 0 ๋ง๊ณ ๋๋จธ์ง ์ซ์๋ค์ ์ ๋ถ ๊ณฑํ ๊ฐ. | ||
- ๊ทธ ์ธ ๋ชจ๋ 0. | ||
- 0์ด ํ๋๋ ์๋ค. | ||
- nums์ ์๋ ๋ชจ๋ ์ซ์๋ค์ ๊ณฑํ ๊ฐ์ p๋ผ๊ณ ํ ๋, ๊ฐ ์ธ๋ฑ์ค i๋ง๋ค p๋ฅผ nums[i]๋ก ๋๋ ๊ฐ. | ||
|
||
๊ทธ๋ฌ๋ฏ๋ก, ๋จผ์ nums์ ์๋ ์ซ์๋ค์ ๋๋ฉด์ | ||
- 0์ด ์๋ ์ซ์๋ฅผ ์ ๋ถ ๊ณฑํ ๊ฐ p๋ฅผ ๋ง๋ ๋ค. | ||
- 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋๋ค. | ||
|
||
๊ทธ ๋ค์ | ||
- 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค์ ๊ธธ์ด๋ฅผ ๋ณด๊ณ ์์ ์ธ ์ํฉ ์ค ํ๋์ ๋์ํด์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฆฌํดํ๋ค. | ||
|
||
SC: | ||
- 0์ด ์๋ ์ซ์๋ค์ ๊ณฑํ ๊ฐ p๋ฅผ ๊ด๋ฆฌํ ๋ O(1) | ||
- 0์ด ๋ฑ์ฅํ๋ ์ธ๋ฑ์ค zero_ind๋ฅผ ๊ด๋ฆฌํ ๋ O(n) | ||
- ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ ๊ฐ O(n) | ||
- ์ข ํฉํ๋ฉด O(n). | ||
|
||
TC: | ||
- nums๊ฐ์ ํ ๋ฒ ์ํํ๋ฉด์ p, zero_ind๊ฐ์ ์ ๋ฐ์ดํธ ํ ๋ O(n) | ||
- ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ ๊ฐ ๋ง๋ค๋ O(n) | ||
- ์ข ํฉํ๋ฉด O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
zero_ind = [] | ||
p = 1 | ||
for i, e in enumerate(nums): | ||
if e == 0: | ||
zero_ind.append(i) | ||
else: | ||
p *= e | ||
|
||
sol = [0] * len(nums) | ||
if len(zero_ind) > 1: | ||
return sol | ||
elif len(zero_ind) == 1: | ||
sol[zero_ind[0]] = p | ||
return sol | ||
else: | ||
return [p // i for i in nums] | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""TC: O(n), SC: O(n) | ||
|
||
์์ด๋์ด: | ||
๋ง์ฝ nums์ ์๋ ์ด๋ค ์ซ์ i์ ๋ํด target - i๋ nums ์์ ์๋ค๋ฉด, ์ด ๋ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ๋ฆฌํดํ๋ฉด ๋๋ค. | ||
๊ทธ๋ฐ๋ฐ ์ด๋ ๊ฒ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์์ธ ์ํฉ์ด ๋ฐ์ํ ์ ์๋ค. | ||
|
||
case 1) nums = [3, 3], target = 6 | ||
- nums์ 3์ด ๋ ๋ฒ ๋์ค๋ฉด i๋ 3์ด๊ณ target - i๋ 3์ธ๋ฐ, ๊ทธ๋ผ ๊ฐ์ ์ซ์์ ๋ํด์ ์ด๋ป๊ฒ ์๋ก ๋ค๋ฅธ ์ธ๋ฑ์ค๋ฅผ | ||
๊ฐ์ ธ์ค๋๊ฐ? | ||
case 2) nums = [2, 3, 4], target = 6 | ||
- nums์ 3์ด ํ๋๋ฐ์ ์๋๋ฐ, i = 3์ผ๋ i๋ nums ์์ ์๊ณ , target - i = 3์ด๋ผ ์ด ๊ฐ๋ nums ์์ ์๋ค. | ||
์๋ก ๋ค๋ฅธ ๋ ์์ดํ ์ ๋ํด์ 6์ด ๋์ด์ผ ํ๋ฏ๋ก [1, 1]๊ฐ์ ๋ต์ ๋ฆฌํดํ๋ฉด ์ค๋ต์ด๋ค. | ||
|
||
์์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค. | ||
- nums์ ์๋ uniqueํ ์ซ์์ ๋ํด ๊ฐ ์ซ์์ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ฅผ dict๋ก ์ ์ฅํ๋ค. | ||
- ๋ง์ฝ nums์ ์ํ๋ ์ด๋ค ์ i์ ๋ํด i์ target - i์ ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ, nums ์์ ์๋ i ์ค์ ์ ์ผ ์์ ์๋ | ||
i์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์์ ์์ ๋ง๋ dict์ ์๋ ์ธ๋ฑ์ค์ ๋น๊ตํด์ ๋ค๋ฅธ์ง ํ์ธํ๋ค. | ||
|
||
Q) ๊ทธ๋ฐ๋ฐ ๋ง์ฝ i, target - i ๊ฐ์ด ์๋ก ๋ค๋ฅผ๋ ๋ ์ค ํ๋๋ผ๋ nums ์์ ๋ ๋ฒ ์ด์ ๋ฑ์ฅํ๋ฉด ์ด๋กํ์ง...? | ||
A) ๊ทธ๋ด ์ผ์ ์๋ค. ์๋ฃจ์ ์ ์ ์ผํ๊ธฐ ๋๋ฌธ์, i๋ target - i๊ฐ ๋ ๋ฒ ์ด์ ๋์ค๋ฉด ์๋ฃจ์ ์ด ๋ ์ด์์ด ๋จ. | ||
|
||
|
||
SC: | ||
- ind_dict๋ฅผ ๋ง๋ค ๋๋ nums_set ๋ง๋ค๋ ๊ฐ๊ฐ O(n). | ||
- ์ฆ, O(n) | ||
|
||
TC: | ||
- ind_dict๋ฅผ ๋ง๋ค๋ O(n). | ||
- nums_set์ ์๋ ๊ฐ์ ๊ฐ์๋งํผ ์ํ, O(n) | ||
- ๋ด๋ถ์์ ์ผ์ด๋๋ ์ผ์ O(1) | ||
- ์ข ํฉํ๋ฉด O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
# ๋จผ์ nums์ ๊ฐ ์์ดํ ๋ง๋ค ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋๋ค. | ||
# ์ฌ๊ธฐ์ ์ฃผ์ํ ๊ฒ์, nums์ ๊ฐ์ ์ซ์๊ฐ ๋ ๋ฒ ์ด์ ๋์ค๋ฉด ๋ง์ง๋ง ์์ดํ ์ ์ธ๋ฑ์ค๊ฐ ๋ค์ด๊ฐ. | ||
ind_dict = {k: i for i, k in enumerate(nums)} # SC: O(n), TC: O(n) | ||
|
||
# nums์ ์๋ uniqueํ ๊ฐ i๋ง๋ค, | ||
for i in (nums_set := set(nums)): # SC: O(n), TC: O(n) | ||
# ๋ง์ฝ target - i๋ nums ์์ ์๋ค๋ฉด, | ||
if target - i in nums_set: | ||
# ๊ทธ๋ฆฌ๊ณ i์ target - i๊ฐ ์๋ก ๋ค๋ฅธ ์ซ์๋ผ๋ฉด, | ||
if i != target - i: | ||
# ์ฌ์ด ์ผ์ด์ค. ๊ทธ๋ฅ ๋ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์์ ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ๋ค. | ||
return [ind_dict[i], ind_dict[target - i]] | ||
|
||
# ์ฌ๊ธฐ์ ๋๋ฌํ๋ค๋ฉด i์ target - i๊ฐ์ด ๊ฐ์. | ||
if (x := nums.index(i)) != ind_dict[target - i]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Walrus ์ฐ์ฐ์๋ฅผ ์ฐธ ์ ์ฐ์๋ ๊ฒ ๊ฐ์์ := There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# ์ฒซ, ๋ง์ง๋ง ๋ฑ์ฅ ์ธ๋ฑ์ค๊ฐ ๋ค๋ฅผ ๊ฒฝ์ฐ, ์ด ๋ ์ซ์๋ฅผ ๋ฆฌํด. | ||
return [x, ind_dict[target - i]] | ||
Comment on lines
+51
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ๋ถ๋ถ walrus ์ข์ ๊ฒ ๊ฐ์ต๋๋ค ์ ๋ ๋ค์์ ์ด๋ ๊ฒ ์จ๋ณด๊ณ ์ถ์ด์ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ํ์ด ๋ณด๊ณ ์ ๋ง๋ค ์ด๊ฑฐ ํผ๋ณด๋์น์์ง ํ๊ณ ๋ ๋ฌธ์ ํ์ด ์ง์ํ์ต๋๋ค