[sangbeenmoon] WEEK 05 Solutions - #2776
Conversation
๐ sangbeenmoon ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
| if sorted_target in d: | ||
| group = d[sorted_target] | ||
| group.append(target) | ||
| d[sorted_target] = group.copy() | ||
| else: | ||
| group = [target] | ||
| d[sorted_target] = group.copy() |
There was a problem hiding this comment.
if sorted_target in d:
d[sorted_target].append(target)
else:
d[sorted_target] = [target]
There was a problem hiding this comment.
์ด๋ ๊ฒ ํ์๋๊ฒ ํจ์ฌ ๋์๊ฑฐ์์, ์ง๊ธ ์ฝ๋์์ copy๋ ์๋ฏธ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ด์
There was a problem hiding this comment.
๋ค ๊น๋ํ๋ค์ ใ ใ ์์ ํ์ต๋๋ค.
| for candidate in d.values(): | ||
| answer.append(candidate) | ||
|
|
||
| return answer |
There was a problem hiding this comment.
list(d.values())
์ด๋ ๊ฒ ๋ฆฌ์คํธ๋ก ํ๋ณํ ํ์๋ฉด answer ์์ด ๋ฐ๋ก ๊ฐ๋ฅํ์ธ์
There was a problem hiding this comment.
๋ค ์์ ํ์ต๋๋ค! ๐
| class TrieNode: | ||
|
|
||
| def __init__(self): | ||
| self.d = {} |
There was a problem hiding this comment.
d๊ฐ ๋ฌด์จ ์ฝ์ด์ผ๊น์?
There was a problem hiding this comment.
dictionary ์ ์ฝ์ด๋ก ์ฌ์ฉํ๋๋ฐ์, ์ข ๋ ๊ฐ๋ ์ฑ ์๊ฒ ๋ค์ด๋ฐ ์์ ํ์ต๋๋ค!
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
|
|
||
| d = {} |
There was a problem hiding this comment.
์๋ฏธ์ ๋ง๋ ๋ค์ด๋ฐ์ ํด์ฃผ์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
๋ค ์์ ํ์ต๋๋ค!
| d[sorted_target] = group.copy() | ||
| else: | ||
| group = [target] | ||
| d[sorted_target] = group.copy() |
There was a problem hiding this comment.
copy ์ฐ์ ์ด์ ๊ฐ ์์๊น์?
There was a problem hiding this comment.
๋ถํ์ํ ๋ถ๋ถ์ด๋ผ ์ ๊ฑฐํ์ต๋๋ค!
| node.insert(word[1:]) | ||
|
|
||
| def search(self, word): | ||
| if word == "": | ||
| return self.is_end | ||
|
|
||
| target = word[0] | ||
|
|
||
| if target in self.d: | ||
| return self.d[target].search(word[1:]) |
There was a problem hiding this comment.
[1:]์ด O(n)์ด๋ผ ํ์ฌ insert, search์์ ํ์ ์ฑ๋ฅ์ด ์ข์ง ์์ ๊ฑฐ ๊ฐ์ต๋๋ค. [1:]์ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌํํด๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
๋ค index ๋ฅผ ๋๊ธฐ๋๋ก ์์ ํ์ต๋๋ค.
| class Trie: | ||
|
|
||
| def __init__(self): | ||
| self.root = TrieNode() | ||
|
|
||
| def insert(self, word: str) -> None: | ||
| self.root.insert(word) | ||
|
|
||
| def search(self, word: str) -> bool: | ||
| return self.root.search(word) | ||
|
|
||
| def startsWith(self, prefix: str) -> bool: | ||
| return self.root.startsWith(prefix) |
There was a problem hiding this comment.
Trie์๋ ๊ตฌํ์ด ์๊ณ TrieNode์ ๋ชจ๋ ๊ตฌํ์ ํ์ ์ด์ ๊ฐ ์์๊น์?
There was a problem hiding this comment.
root ์ฒ๋ฆฌ๊ฐ ๊น๋ํ ๊น ์ถ์ด์ TrieNode ์ ๊ตฌํํ๋ ๊ฒ์ธ๋ฐ์, Trie ์ ์ง์ ๊ตฌํํ๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ด์๋ค์ ใ ใ ์์ ํ์ต๋๋ค!
parkhojeong
left a comment
There was a problem hiding this comment.
์ด๋ฒ ์ฃผ์ฐจ ํ์ด์ ๊ด๊ณ ์๋ ์ปค๋ฐ ๋ก๊ทธ๋ ์ ๋ฆฌ ๋ถํ๋๋ฆฌ๊ณ ๋ฆฌ๋ทฐ ์ปค๋ฉํธ๋ ํ์ธ ๋ถํ๋๋ฆฝ๋๋ค.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
- ํจํด: Hash Map / Hash Set, Sorting
- ์ค๋ช : ์ฒซ ๋ฒ์งธ ํ์ด์์ ๊ฐ ๋ฌธ์์ด์ ์ ๋ ฌํ ๊ฒฐ๊ณผ๋ฅผ ํค๋ก ์ผ์ ๊ทธ๋ฃนํํ๋ ค๊ณ ํ๋ฉฐ, ๋ ๋ฒ์งธ ํ์ด๋ ์ ๋ ฌ๋ ๋ฌธ์์ด์ ํค๋ก ์ฌ์ ์ ๋งคํํ์ฌ ๊ฐ์ ์๋๊ทธ๋จ ๋ฌถ์์ ๊ตฌ์ฑํฉ๋๋ค. ํจ์จ์ ์ธ ๊ทธ๋ฃนํ๋ฅผ ์ํด ํด์ ๋งต์ ์ฌ์ฉํฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n * k log k) |
| Space | O(n * k) |
ํผ๋๋ฐฑ: ๋ ๊ฐ์ง ๊ตฌํ ๋ชจ๋ ์๋ก ๋ค๋ฅธ ์๊ฐ ๋ณต์ก๋๋ก ๊ทธ๋ฃน์ ์ฐพ์ง๋ง ๋ ๋ฒ์งธ ๊ตฌํ์ด ์ผ๋ฐ์ ์ผ๋ก ๋ ๊ฐ๋จํ๊ณ ํจ์จ์ ์ด๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
- ํจํด: Trie, Depth-First Search, Hash Map / Hash Set
- ์ค๋ช : ํธ๋ผ์ด ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ ๋ฌธ์์ด ๊ตฌ์ฑ ์์ ํ์์ผ๋ก ๊ฐ ๋ ธ๋์ ์์ ๋ฐ ๋ ์ฌ๋ถ๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๊ด๋ฆฌํฉ๋๋ค. ์ฝ์ /๊ฒ์/์ ๋์ด ํ์์ ๊น์ด ์ฐ์ ๋ฐฉ์์ผ๋ก ํธ๋ฆฌ๋ฅผ ๋ฐ๋ผ ๋ด๋ ค๊ฐ๋ฉฐ ๊ตฌํ๋ฉ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(L) |
| Space | O(์ํ๋ฒณ ์ * ๋ ธ๋ ์) |
ํผ๋๋ฐฑ: ํธ๋ผ์ด ๊ตฌ์กฐ๋ฅผ ์ฌ๊ท๋ก ๊ตฌํํ์ผ๋ฉฐ ๊ฐ ํธ์ถ์ด ๋จ์ ๋ฌธ์์ด ๊ธธ์ด์ ๋น๋กํ๋ค.
๊ฐ์ ์ ์: ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ์ ์ํด ํด๋์ค ๋ฉค๋ฒ ๋ณ์๋ฅผ ์ธ์คํด์ค๋ณ๋ก ๊ด๋ฆฌํ๊ณ ์ต์ ํํ ์ ์๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
- ํจํด: Dynamic Programming, Hash Map / Hash Set
- ์ค๋ช : ๋ถ๋ถ ๋ฌธ์์ด๋ก ๊ฐ๋ฅํ ๋จ์ด๋ฅผ ํ์ธํ๋ฉฐ DP + ์ฌ๊ท ํ์์ผ๋ก ๋ถ๋ถ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ ๊ตฌ์กฐ์, ๋จ์ด ์งํฉ์ ํด์ ๋งต์ผ๋ก ๋น ๋ฅด๊ฒ ์กฐํํ๋ ํจํด์ด ํต์ฌ์ ๋๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(n^2) |
| Space | O(n) |
ํผ๋๋ฐฑ: ์ฃผ์์ ์คํจํ ํ์ด๋ฅผ ์ ๊ฑฐํ๊ณ ์ค์ DP ํ์ด๊ฐ ํฌํจ๋์ด ์๋ค. ํ์ฌ ๊ตฌํ์ ์ฌ๊ท๋ฅผ ์ฌ์ฉํ ํ์์ผ๋ก ๊ฐ๋ ์ฑ์ ์ข์ผ๋ ์ต์ ์ ๊ฒฝ์ฐ ์๊ฐ๋ณต์ก๋์ ์ฃผ์๊ฐ ํ์ํ๋ค.
๊ฐ์ ์ ์: ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ์ด์ ์ด์ (dp ๋ฐฐ์ด)๊ณผ ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ ๋ ๋ช ํํ ํ์ฌ ๋ถํ์ํ ์ฌํ์์ ์ค์ด๋๋ก ๊ฐ์ ํด๋ณด์.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!