Skip to content
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

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions climbing-stairs/haklee.py
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
Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ํ’€์ด ๋ณด๊ณ  ์•„ ๋งž๋‹ค ์ด๊ฑฐ ํ”ผ๋ณด๋‚˜์น˜์˜€์ง€ ํ•˜๊ณ ๋Š” ๋ฌธ์ œํ’€์ด ์ง€์›ํ–ˆ์Šต๋‹ˆ๋‹ค

51 changes: 51 additions & 0 deletions coin-change/haklee.py
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)
114 changes: 114 additions & 0 deletions combination-sum/haklee.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp[target]์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” target์˜ ์ตœ๋Œ€ ํฌ๊ธฐ / candidates์˜ ์ตœ์†Œ ๊ฐ’์ด๋ฏ€๋กœ 40 / 2 = 20
list * set * tuple * sorted ํ•˜๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(m * m * n * nlogn) ์ด๋‹ˆ ๋ฌธ์ œ์—์„œ candidates์˜ ์›์†Œ๋“ค์ด distinctํ•˜๋‹ค๋Š” ์ ์„ ๋ฌด์‹œํ•˜๊ณ  ์–ด๋ฆผํ•ด๋„ ๋Œ€๋žต 20๋งŒ ์ •๋„๋กœ ์ถฉ๋ถ„ํžˆ ์•ˆ์ „ํ•œ ๊ฐ’์ด๊ธด ํ•˜์ง€๋งŒ target์ด๋‚˜ candidates๊ฐ€ ์ปค์ง€๋ฉด ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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]] # ๋ฐฉ๋ฒ•๋งŒ ์ถ”์ถœํ•ด์„œ ๋ฆฌํ„ดํ•œ๋‹ค.
149 changes: 149 additions & 0 deletions product-of-array-except-self/haklee.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌด์„ญ์Šต๋‹ˆ๋‹ค... ์ œ ์ƒ๊ฐ์— ์ด ๋ฌธ์ œ์˜ ์˜๋„๋Š” ํฌ๊ฒŒ๋Š” ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ํฌ์ƒํ•ด์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์–ด๋–ป๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ๊ฒƒ์ธ๊ฐ€, ์ข๊ฒŒ๋Š” prefix์™€ suffix๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌ๊ฐ„์˜ ๊ณฑ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด์„œ ์ดํ›„ ์—ฐ์‚ฐ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉํ–ฅ์œผ๋กœ ์ƒ๊ฐํ•ด์„œ ๋‹ค๋ฅธ ํ’€์ด๋„ ๋„์ „ํ•ด๋ณด์‹œ๋ฉด ์–ด๋–จ๊นŒ์š”

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
]
53 changes: 53 additions & 0 deletions two-sum/haklee.py
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]:
Copy link
Contributor

@DaleSeo DaleSeo Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Walrus ์—ฐ์‚ฐ์ž๋ฅผ ์ฐธ ์ž˜ ์“ฐ์‹œ๋Š” ๊ฒƒ ๊ฐ™์•„์š” :=

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์™œ walrus์ธ๊ฐ€ ํ–ˆ๋”๋‹ˆ ์ง„์งœ ๋ฐ”๋‹ค์ฝ”๋ผ๋ฆฌ๋ฅผ ๋‹ฎ์•˜๊ตฐ์š” :D
image

# ์ฒซ, ๋งˆ์ง€๋ง‰ ๋“ฑ์žฅ ์ธ๋ฑ์Šค๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ, ์ด ๋‘ ์ˆซ์ž๋ฅผ ๋ฆฌํ„ด.
return [x, ind_dict[target - i]]
Comment on lines +51 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„ walrus ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ์ €๋„ ๋‹ค์Œ์— ์ด๋ ‡๊ฒŒ ์จ๋ณด๊ณ  ์‹ถ์–ด์š”