-
-
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 all commits
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,149 @@ | ||
"""TC: O(n), SC: O(1) | ||
|
||
์์ด๋์ด: | ||
๋ค์์ ์ธ ์ํฉ ์ค ํ๋๊ฐ ๋ ๊ฒ์ด๋ค. | ||
- 0์ด ๋ ์ด์ ํฌํจ. ๋ชจ๋ ๊ฒฐ๊ณผ๊ฐ์ด 0์ด ๋๋ค. | ||
- 0์ด ํ๋ ํฌํจ. | ||
- 0์ ํด๋นํ๋ ์ธ๋ฑ์ค์๋ nums์ ์๋ ์ซ์๋ค ์ค 0 ๋ง๊ณ ๋๋จธ์ง ์ซ์๋ค์ ์ ๋ถ ๊ณฑํ ๊ฐ. | ||
- ๊ทธ ์ธ ๋ชจ๋ 0. | ||
- 0์ด ํ๋๋ ์๋ค. | ||
- nums์ ์๋ ๋ชจ๋ ์ซ์๋ค์ ๊ณฑํ ๊ฐ์ p๋ผ๊ณ ํ ๋, ๊ฐ ์ธ๋ฑ์ค i๋ง๋ค p๋ฅผ nums[i]๋ก ๋๋ ๊ฐ. | ||
|
||
๊ทธ๋ฌ๋ฏ๋ก, ๋จผ์ nums์ ์๋ ์ซ์๋ค์ ๋๋ฉด์ | ||
- 0์ด ์๋ ์ซ์๋ฅผ ์ ๋ถ ๊ณฑํ ๊ฐ p๋ฅผ ๋ง๋ ๋ค. | ||
- 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋๋ค. | ||
- 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค๊ฐ ๋ ์ด์์ธ์ง ์ฒดํฌํ๋ค. | ||
|
||
๊ทธ ๋ค์ | ||
- 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค๊ฐ ๋ ์ด์์ธ์ง, 0์ด ๋ค์ด์๋ ์ธ๋ฑ์ค๊ฐ ์๋์ง ๋ณด๊ณ ์ํฉ์ ๋ง๊ฒ ๊ฒฐ๊ณผ๊ฐ์ ๋ฆฌํด. | ||
|
||
SC: | ||
- 0์ด ์๋ ์ซ์๋ค์ ๊ณฑํ ๊ฐ p๋ฅผ ๊ด๋ฆฌํ ๋ O(1) | ||
- 0์ด ๋ฑ์ฅํ๋ ์ธ๋ฑ์ค zero_ind์ ๋ ์ด์ ์๋์ง ์ฌ๋ถ all_zero ํ๋๊ทธ๋ฅผ ๊ด๋ฆฌํ ๋ O(1). | ||
- ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ ๊ฐ O(n) <- ์ด๊ฑด ๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์์์ ์ ์ธํ๋ค. | ||
- ์ข ํฉํ๋ฉด O(1). | ||
|
||
TC: | ||
- nums๊ฐ์ ํ ๋ฒ ์ํํ๋ฉด์ p, zero_ind, all_zero๊ฐ์ ์ ๋ฐ์ดํธ ํ ๋ O(n) | ||
- ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ ๊ฐ ๋ง๋ค๋ O(n) | ||
- ์ข ํฉํ๋ฉด O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
all_zero = True | ||
zero_ind = None | ||
p = 1 | ||
for i, e in enumerate(nums): | ||
if e == 0: | ||
if zero_ind is not None: | ||
break | ||
zero_ind = i | ||
else: | ||
p *= e | ||
else: | ||
all_zero = False | ||
sol = [0] * len(nums) | ||
if all_zero: | ||
return sol | ||
elif zero_ind is not None: | ||
sol[zero_ind] = p | ||
return sol | ||
else: | ||
return [p // i for i in nums] | ||
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
""" TC: O(n), SC: O(1) | ||
ํ์ง๋ง ์ ์๋ฃจ์ ์ ๋ฌธ์ ์์ ์ฌ์ฉํ์ง ๋ง๋ผ๊ณ ํ `/`(์ ํํ๋ `//`)๋ฅผ ์ผ๋ค๋ ๋ฌธ์ ๊ฐ ์๋ค. | ||
๋๋๊ธฐ๋ฅผ ํ์ง ๋ง๋ผ๊ณ ํ์ผ๋ ์ด๋ฅผ ์ด๋ป๊ฒ ์ ์ฐํํด๋ณด์. | ||
|
||
โป ์ฃผ์: ์ ๋ฐํ ๊ณ์ฐ์ ์ทจ์ฝํ ์ ๊ทผ ๋ฐฉ๋ฒ์ด๋ฏ๋ก ๊ตฌํํ ๊ฒ์ด ์ ์๋ํ ๊ฒ์ด๋ผ๋ ์ฌ์ค์ | ||
์ ๋ง ์์ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฉด ์ด๋ ๊ฒ ๊ตฌํํ๋ฉด ์๋๋ค. | ||
|
||
์์ด๋์ด: | ||
๋ค์์ ๋ฑ์์ ๋ณด์. | ||
|
||
x / y = 2^(log2(x)) / 2^(log2(y)) = 2^(log2(x)-log2(y)) | ||
|
||
์ฆ, ์ฐ๋ฆฌ๋ ๋๋๊ธฐ ์ฐ์ฐ์ power, log, - ์ฐ์ฐ์ผ๋ก ์ฐํ ๊ฐ๋ฅํ๋ค. | ||
Comment on lines
+64
to
+69
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. ๋ฌด์ญ์ต๋๋ค... ์ ์๊ฐ์ ์ด ๋ฌธ์ ์ ์๋๋ ํฌ๊ฒ๋ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ ํฌ์ํด์ ์๊ฐ๋ณต์ก๋๋ฅผ ์ด๋ป๊ฒ ํฅ์์ํฌ ๊ฒ์ธ๊ฐ, ์ข๊ฒ๋ prefix์ suffix๋ฅผ ์ฌ์ฉํด์ ๊ตฌ๊ฐ์ ๊ณฑ์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ฅํด์ ์ดํ ์ฐ์ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ผ ์๊ฐํฉ๋๋ค. ์ด๋ฐ ๋ฐฉํฅ์ผ๋ก ์๊ฐํด์ ๋ค๋ฅธ ํ์ด๋ ๋์ ํด๋ณด์๋ฉด ์ด๋จ๊น์ 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. ๋์ ํฉ์ ์ฐ๋ ๊ฒ๊ณผ ๋น์ทํ ๋ฐฉ์์ผ๋ก ํธ๋ ๊ฒ๋ ์ข์๊ฒ ์ง๋ง, ์ด๋ฒ์๋ ์ด๋ ๊ฒ ๋ฌธ์ ๋ฅผ ๋ซ์ด๋ณธ ๊ฒ์ผ๋ก ๋ง์กฑํ๊ณ ์ฌ์ด๊ฐ๋ณด๊ฒ ์ต๋๋ค.. |
||
|
||
์์ ์ฐํ ๋ฐฉ๋ฒ์ ํ์ฉํ๊ธฐ ์ํด ๊ธฐ์กด์ ์ ๊ทผ ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ค. | ||
- ์๋๋ p๋ฅผ 1๋ก ์ด๊ธฐํํ ๋ค์ nums์ ์๋ ๊ฐ๋ค ์ค 0์ด ์๋ ๊ฒ๋ค์ ์ ๋ถ ๊ณฑํด์คฌ๋ค. | ||
- ์ด์ p์๋ 2^p์ ๊ณ์ฐํ์๋ ๊ณฑ์ ์ ๊ฒฐ๊ณผ๊ฐ์ด ๋์ค๋ ๊ฐ์ ์ ์ฅํ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก, | ||
p๋ฅผ 0์ผ๋ก ์ด๊ธฐํํ๊ณ p์๋ nums์ ์๋ ๊ฐ๋ค ์ค 0์ด ์๋ ๊ฐ์ log๋ฅผ ์ทจํ ๊ฐ์ ๋ํด์ค๋ค. | ||
|
||
ํ์ง๋ง 0์ดํ์ ์ค์ x์ ๋ํด์๋ log(x)๊ฐ ์ ์๋์ง ์๋ ๋ฌธ์ ๊ฐ ์๋ค. | ||
๊ทธ๋ฌ๋ฏ๋ก ๋ค์๊ณผ ๊ฐ์ ์กฐ์น๋ฅผ ์ทจํด์ผ ํ๋ค. | ||
- nums์ ์๋ ๊ฐ x๊ฐ 0์ด๋ฉด ๊ทธ๋ฅ ๋์ด๊ฐ๋ค. ์ด๋ ๊ธฐ์กด์ ์ ๊ทผ ๋ฐฉ๋ฒ๊ณผ ๋์ผํ๋ค. | ||
- nums์ ์๋ ๊ฐ x๊ฐ 0๋ณด๋ค ์์ผ๋ฉด ์ ๋๊ฐ์ ์ทจํ๊ณ ๋ก๊ทธ๋ฅผ ์์ด๋ค. | ||
์ฆ, log(|x|)๋ฅผ ๊ณ์ฐํ๋ค. | ||
- ์ด ๊ฒฝ์ฐ ๊ฒฐ๊ณผ์๋ -1๋ ๊ณฑํด์ค์ผ ํ๋ค๋ ์ฌ์ค์ ์์์ผ ํ๋ฏ๋ก ์ด ์ ๋ณด๋ฅผ | ||
`is_neg`๋ผ๋ ๋ณ์๋ก ๊ด๋ฆฌํ์. | ||
|
||
์ฃผ์์ : | ||
์ค๋ช ์ ๋ฑ์ฅํ๋ ๊ธฐํธ์ ์๋์ ์ฝ๋์ ๋ฑ์ฅํ๋ ๊ธฐํธ๋ฅผ ํท๊ฐ๋ฆฌ๋ฉด ์๋๋ค. | ||
- ์์์๋ power๋ฅผ `^`๊ธฐํธ๋ก ์ผ์ง๋ง python์์๋ `**`๊ธฐํธ๋ฅผ ์ด๋ค. | ||
- ๊ทธ๋ฆฌ๊ณ python์์ `^`๊ธฐํธ๋ xor์ ์๋ฏธํ๋ค. | ||
|
||
๋ฌธ์ ์ : | ||
- ์์ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์ด๋ก ์ ์ผ๋ก๋ ๋ฌธ์ ๋ ๊ฒ์ด ์์ง๋ง ์ํ๊น๊ฒ๋ ์ปดํจํฐ๋ฅผ ํตํ ์ฐ์ฐ์์๋ | ||
log๋ฅผ ๊ณ์ฐํ ๊ฐ์ ์์์ ๋ท ์๋ฆฌ์๋ค์ด ์๋ ค๋๊ฐ๋ค. | ||
- ์ด๋ก ์ธํด 2^p๋ฅผ ๊ณ์ฐํ ๊ฒฐ๊ณผ๊ฐ์ด ๊น๋ํ ์ ์๊ฐ ์๋๋ผ ๋๋ฌ์ด ์์๊ฐ ๋์จ๋ค. ๊ทธ๋์ | ||
์ด ์ซ์๊ฐ ์ ๋ต์ ๊ทผ์ ํ ๊ฐ์ผ ๊ฒ์ด๋ผ๊ณ ๊ตณ๊ฒ ๋ฏฟ๊ณ ์ฌ๊ธฐ์ `round` ํจ์๋ฅผ ์จ์ ๋ฐ์ฌ๋ฆผ์ | ||
ํด์ผ ์ํ๋ ๋ต์ด ๋์จ๋ค. | ||
- ๊ทธ๋ฐ๋ฐ ์๊ฐํด๋ณด๋ฉด nums์ ๋ค์ด์๋ ๊ฐ๋ค์ ๋ก๊ทธ๋ฅผ ์ทจํ๊ณ ๋ํ๋ ๊ณผ์ ์์ ์ด ์๋ฆฐ ์์์ | ||
๊ฐ๋ค์ด ์ ์ ๋์ ๋๋ฉด์ ์ค์ฐจ๊ฐ ์ ์ ์ปค์ง ๊ฒ์ด๋ค. ์ค์ฐจ๊ฐ ์ถฉ๋ถํ ์ปค์ง๋ฉด 2^p๋ฅผ ๊ณ์ฐํ ๊ฐ์ | ||
๋ฐ์ฌ๋ฆผ ํ๋๋ผ๋ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ์ ํํ ๋ต์ด ๋์ค์ง ์๊ฒ ๋ ๊ฒ์ด๋ค. | ||
- e.g.) nums = range(2, 18) ์ผ๋ | ||
- expected answer: [177843714048000, 118562476032000, 88921857024000, 71137485619200, ...] | ||
- result: [177843714047999, 118562476031999, 88921857023999, 71137485619200, ...] | ||
- diff: [-1, -1, -1, 0, ...] | ||
- ๊ณฑํ๋ ์์ ํฐ ์๊ฐ ์์ฌ๋ ๋ฌธ์ ๊ฐ ์ฝ๊ฒ ๋ฐ์ํ๋ค. | ||
- e.g.) nums = [2, 10, 44444444444444] ์ผ๋ | ||
- expected answer: [444444444444440, 88888888888888, 20] | ||
- result: [444444444444441, 88888888888888, 20] | ||
- diff: [1, 0, 0] | ||
- ๊ทธ๋ ๋ค๋ฉด ์ผ๋ง๋ ๋ง์ ์ซ์๋ฅผ ๊ณฑํ๊ฑฐ๋ ํฐ ์ซ์๋ฅผ ๊ณฑํ๋ ์ํฉ์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋๊ฐ? | ||
๋ฌธ์ ์ ์ฃผ์ด์ง ์กฐ๊ฑด ๋ฒ์ ๋ด์์ ์์ ๋ฐฉ๋ฒ์ด ์ ์๋ํ ๊ฒ์ด๋ผ๋ ์ฌ์ค์ด ๋ณด์ฅ๋๋๊ฐ? | ||
์ด์ ๋ํด ์๊ฐํ๋ ๊ฒ์ ๋งค์ฐ ๊ท์ฐฎ์ ์ผ์ด๋ค... | ||
|
||
๊ทธ๋ฐ๋ฐ | ||
- ๋์ถฉ ๊ด์ฐฐ์ ํด๋ณด๋ ์ซ์๋ค์ ๊ณฑ์ด ๋ฌธ์ ์์ ์ฃผ์ด์ง ์กฐ๊ฑด์ธ `The product ... fit in a 32-bit integer.` | ||
๋ณด๋ค ํจ์ฌ ํฐ ๊ฒฝ์ฐ์๋ง ์์ ์ค์ฐจ๊ฐ ์น๋ช ์ ์ธ ์ํฅ์ ์ค๋ค. | ||
- ๊ทธ๋์ ์ผ๋จ ๋ฆฌํธ์ฝ๋์ ํ์ด๋ฅผ ๋์ ธ๋ณด์๋๋ accept ๋์๋ค. ๋๋ leetcodeํผ์ ์ ํ์๋ค๊ณ | ||
๋ณผ ์ ์๋ ๊ฒ์ด๋ค. | ||
- ๋ง์ฝ ์ด ํ์ด๊ฐ ์๋ชป๋์๋ค๊ณ ๋งํ๊ณ ์ถ๋ค๋ฉด ๋ ๋ง๊ณ ๋ฌธ์ ์กฐ๊ฑด์ ์ค๊ณํ ์ฌ๋๊ณผ ํ ์ผ๋ฅผ ๋ง๋ | ||
์ฌ๋๋ค์๊ฒ ๋์ ๋์ ธ๋ผ ยฏ\_(ใ)_/ยฏ | ||
""" | ||
|
||
import math | ||
|
||
|
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
all_zero = True | ||
zero_ind = None | ||
p, is_neg = 0, False | ||
for i, e in enumerate(nums): | ||
if e == 0: | ||
if zero_ind is not None: | ||
break | ||
zero_ind = i | ||
else: | ||
is_neg ^= e < 0 | ||
p += math.log2(abs(e)) | ||
else: | ||
all_zero = False | ||
|
||
sol = [0] * len(nums) | ||
if all_zero: | ||
return sol | ||
elif zero_ind is not None: | ||
sol[zero_ind] = round(2**p * (-1) ** (is_neg)) | ||
return sol | ||
else: | ||
return [ | ||
round(2 ** (p - math.log2(abs(i))) * (-1) ** (is_neg ^ (i < 0))) | ||
for i in nums | ||
] |
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.
์ด ํ์ด ๋ณด๊ณ ์ ๋ง๋ค ์ด๊ฑฐ ํผ๋ณด๋์น์์ง ํ๊ณ ๋ ๋ฌธ์ ํ์ด ์ง์ํ์ต๋๋ค