Repository navigation
[Chaewon-Park-STUDY] WEEK 04 Solutions - #2966
Chaewon-Park-STUDY wants to merge 15 commits into
Conversation
|
๐ค AI ๋ฆฌ๋ทฐ(์๊ณ ๋ฆฌ์ฆ ํจํด, ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋, ํ์ต ํํฉ) ๊ฒฐ๊ณผ๋ Checks ํญ์์ ๋ณผ ์ ์์ด์. |
|
|
||
|
|
||
| # ํ์ด1) (๋ค๋ฅธ ํ์ด ๋ ๊ณ ๋ฏผ ์์ ) | ||
| #Time:O(n+m),Space:O(n+m) n:list1์ ๋ ธ๋์ ๊ฐ์ m:list2์ ๋ ธ๋ ๊ฐฏ์ |
There was a problem hiding this comment.
๋ ๋ฆฌ์คํธ๊ฐ ์ด๋ฏธ ์ ๋ ฌ๋์ด ์๋ค๋ ์ ์ ํ์ฉํด head๋ผ๋ฆฌ ๋น๊ตํ๊ณ , ์์ ๊ฐ๋ถํฐ ๋ณํฉํ๋ ์์ด๋์ด๊ฐ ์ข์์ต๋๋ค. ์๊ฐ ๋ฐ ๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์๋ ๋ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
๋ํ, ์ด ๋ถ๋ถ ์ญ์ Space Optimisation์์์ ํฐ ์๋ฆฌ์๋ ์ด๋ ์ ๋ ๋ง์ต๋๋ค.
ํ์ฌ๋ answer ๋ฐฐ์ด์ ๋ชจ๋ ๋
ธ๋์ ๊ฐ์ ์ ์ฅํ ๋ค, ์๋ก์ด ListNode๋ฅผ ์์ฑํ๊ณ ์๋๋ฐ์.
ํน์ ๋ณ๋์ ๋ฐฐ์ด์ ๊ฐ์ ์ ์ฅํ์ง ์๊ณ , ๋น๊ตํ๋ ์์ ์ ๊ธฐ์กด ListNode์ next ์ฐธ์กฐ๋ฅผ ์ง์ ์ฐ๊ฒฐํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ ์ ์์๊น์?
๊ธฐ์กด ๋ณํฉ ์์ด๋์ด๋ฅผ ๊ทธ๋๋ก ์ ์งํ๋ฉด์๋ ์ถ๊ฐ ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ O(1)๊น์ง ์ค์ผ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
๋ฆฌ๋ทฐ ๊ฐ์ฌํฉ๋๋ค! ๐
์ ๋ ๋ค๋ฅธ ํจ์จ์ ํ์ด๊ฐ ์์ ๊ฒ ๊ฐ์ ๊ณ ๋ฏผํด๋ณด๋ ค๊ณ ํ์๋๋ฐ, ๋ง์ํด ์ฃผ์ ๊ฒ์ฒ๋ผ ๊ฐ์ ๋ฐ๋ก ์ ์ฅํ์ง ์๊ณ ๋น๊ตํ๋ ์์ ์ ๊ธฐ์กด ๋
ธ๋์ next๋ฅผ ์ง์ ์ฐ๊ฒฐํ๋ ๋ฐฉ์์ผ๋ก ํ์ด๋ณด๊ฒ ์ต๋๋ค:)
There was a problem hiding this comment.
ํ์คํ ๋ ธ๋ ๊ฐ ์ฐ๊ฒฐ๋ก ์ฝ๋๋ฅผ ์์ ํ๋ ๊ต์ฅํ ๊ฐ๊ฒฐํด์ง๋ค์~!
parkhojeong
left a comment
There was a problem hiding this comment.
์๊ณ ํ์ จ์ต๋๋ค! ๋ช๊ฐ์ง ์ปค๋ฉํธ ๋จ๊ฒผ์ต๋๋ค~
| for k in range(4): | ||
| nx, ny = x + dxs[k], y + dys[k] | ||
| if in_range(nx, ny) and (nx, ny) not in path and board[nx][ny] == word[index + 1]: | ||
| stack.append((nx, ny, index + 1, path | {(nx, ny)})) # ๊ฐ๋ฆผ๊ธธ๋ง๋ค ์ ์งํฉ |
There was a problem hiding this comment.
ํ์ฌ๋ ์๋ก์ด ๊ฒฝ๋ก๋ฅผ ํ์ํ ๋๋ง๋ค ์งํฉ์ ๋ณต์ฌํ๊ณ ์์ด ์ถ๊ฐ์ ์ธ ์๊ฐ ๋ฐ ๊ณต๊ฐ ๋น์ฉ์ด ๋ฐ์ํ๋๋ฐ์. ์ด ๋ถ๋ถ์ ๊ฐ์ ํ ์ ์๋์ง ๊ณ ๋ฏผํด ๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
๋ค๋ฅธ ๋ถ ํ์ด์์ ๋น์ทํ๊ฒ ๋ณธ ๊ฒ ๊ฐ์๋ฐ ์ข ๋ ๊ณ ๋ฏผํด๋ณด๊ณ ์์ ํ๊ฒ ์ต๋๋ค ใ
ใ
๊ฐ์ฌํฉ๋๋ค
| last.next = list2 # list2 ์ฐ๊ฒฐ | ||
| list2 = list2.next | ||
| last = last.next # ๊ฒฐ๊ณผ์ ๋ง์ง๋ง ๋ ธ๋๋ก ์ด๋ | ||
| last.next = list1 or list2 # ๋จ์ ๋ฆฌ์คํธ๋ฅผ ํต์งธ๋ก ์ฐ๊ฒฐ |
There was a problem hiding this comment.
์ผํญ์ฐ์ฐ์๋ณด๋ค list1 or list2 ๋ก ํ๋ฉด ๋ ๊ฐ๊ฒฐํ๋ค์. ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค.
There was a problem hiding this comment.
์ ๋ None ๋ค๋ฃจ๋ ๊ฒ ์์ง ์์ ์ต์ํ์ง๋ ์์๋ฐ, ์์ฆ ์ต๋ํ ๊ฐ๊ฒฐํ๊ฒ ์จ๋ณด๋ ค๊ณ ์ด๋ ๊ฒ ํ๊ณ ์์ต๋๋ค!
| @@ -0,0 +1,15 @@ | |||
| # ํ์ด1) 1์ฐจ์ DP: dp[i] = i์์ ๋ง๋๋ ์ต์ ๋์ ์ | |||
| # ์๊ฐ ๋ณต์ก๋: O(๊ธ์ก ร ๋์ ์ข ๋ฅ ์), ๊ณต๊ฐ ๋ณต์ก๋: O(๊ธ์ก) | |||
| class Solution: | |||
There was a problem hiding this comment.
์ ๋ float('inf')๋ก ํ์ต๋๋ค๋ง 10**10 ์ผ๋ก ์ด๊ธฐํํ ๋ถ๋ถ์ด ์ธ์์ ์ด๋ค์.
์ฐพ์๋ณด๋ ๋ชจ๋ ๊ฐ๋ฅํ ๋์ ๊ฐ์ ๋ณด๋ค ํญ์ ํฐ ๊ฐ์ฅ ์์ ๋์ฒด๊ฐ์ผ๋ก amount + 1๋ก ์ด๊ธฐํ๋ฅผ ํด๋ ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
๋๋ถ์ ํ์ต๋์์ต๋๋ค.
There was a problem hiding this comment.
์ ๋ ์คํ๋ ค float('inf')๊ฐ ๋ ๋ช
์์ ์ธ ๊ฒ ๊ฐ๋ค์. 10**10์ ๋งค๋ฒ ์ง์ ์
๋ ฅํ๊ธฐ๋ ๋ฒ๊ฑฐ๋กญ๊ณ , ํน์ ๋ฌธ์ ์กฐ๊ฑด์์ ์ ํ ๋ฒ์์ ๋ค์ด๊ฐ๋ฉด ํ๋ฆด ์๋ ์์ด์ inf๋ก ๋๋ ๊ฒ ๋ ์์ ํ ๊ฒ ๊ฐ๋ค์.
amount + 1๋ก ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ์ ๋ ๋ฐฐ์๊ฐ๋๋ค:) ๋ฆฌ๋ทฐ ๊ฐ์ฌํฉ๋๋ค
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!