-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Hyun] Week 9 Solution Explanation #154
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
Conversation
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.
์๊ณ ํ์ จ์ต๋๋ค!
func search(_ word: String) -> Bool { | ||
let word = Array(word) | ||
func dfs(index: Int, node: Node) -> Bool { | ||
guard index < word.count else { return node.endOfWord } | ||
|
||
let character = word[index] | ||
if character == "." { | ||
return node.children.values.contains { dfs(index: index + 1, node: $0) } | ||
} else if let nextNode = node.children[character] { | ||
return dfs(index: index + 1, node: nextNode) | ||
} | ||
return false | ||
} | ||
return dfs(index: 0, node: root) | ||
} |
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.
Trie๋ฅผ ๊ตฌ์ฑํ์ฌ ํ์ดํ์ต๋๋ค.
Trie๋ก ๊ฒ์ํ๋ ๊ตฌ๊ฐ์์ .์ด ๋ค์ด์ค๋ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ํ์ ํด์ฃผ์ด์ผํ๊ธฐ ๋๋ฌธ์, ๋ณด๋ค ์ข ๋ ์๊ฐ์ด ๊ฑธ๋ฆด ๊ฒ์ ๋๋ค.
์ด ๋ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ์ ์ด๋ป๊ฒ ํด์ผํ ์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค์ ๐ ํน์ ์๊ฒฌ์ด ์์ผ์๋ค๋ฉด ์ฒจ์ธ ๋ถํ๋๋ฆฌ๊ฒ ์ต๋๋ค.
47๋ฒ์งธ ์ค์ dfs()
ํจ์ ์ฌ๊ท ํธ์ถ์ด ์ต์
์ ๊ฒฝ์ฐ 26๋ฒ ์ผ์ด๋ ์ ์์ ๊ฒ์
๋๋ค. ์๋ํ๋ฉด ๋ฌธ์ ์์ ๋จ์ด์๋ ์์ด ์๋ฌธ์๋ง ๋ค์ด์์ ์ ์๋ค๊ณ ํ๊ธฐ ๋๋ฌธ์
๋๋ค. ์ฆ, ๊ฐ ๊ธ์์์ ๋ค์ ๊ธ์๊ฐ ๋ ์ ์๋ ๊ฒฝ์ฐ์ ์๊ฐ ์๋ฌด๋ฆฌ ๋ง์๋ 26๊ฐ๋ ๋์ ์ ์๋ ๊ฒ์ด์ง์. ๊ทธ๋ฆฌ๊ณ ํธ์ถ ์คํ์ ๊ฒ์ํ๋ ค๋ ๋จ์ด์ ๊ธธ์ด์ ๋น๋กํด์ ๊น์ด์ง ๊ฒ์
๋๋ค. ์ฆ, ๋จ์ด๊ฐ 5๊ฐ์ ๊ธ์๋ก ์ด๋ฃจ์ด์ ธ ์๋ค๋ฉด, ์ต์
์ ๊ฒฝ์ฐ dfs()
ํจ์๋ฅผ 26 * 26 * 26 * 26 * 26 = 26^5
๋ฒ ํธ์ถํด์ผ ํธ๋ผ์ด์์ ์ฐพ์๋ผ ์ ์์ ๊ฒ์
๋๋ค.
๋ฐ๋ผ์ search()
๋ฉ์๋์ ์๊ฐ ๋ณต์ก๋๋ O(26^w)
๊ฐ ๋๊ณ , ๊ณต๊ฐ ๋ณต์ก๋๋ O(w)
๊ฐ ๋ ๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง์?
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.
์ ๋ ๋์ผํ๊ฒ ์๊ฐํฉ๋๋ค. Big-O ํ๊ธฐ๋ฒ์์ 26^n๊ณผ ๊ฐ์ด ํ๊ธฐํ๋ ๊ฒ๋ ๊ด์ฐฎ์๊น ์ถ์ด์ ์ฐ์ n์ผ๋ก ํ๊ธฐํด๋ณด์์ต๋๋ค. O(26^w)
์ ๊ฐ์ด ํ๊ธฐํด๋ ๊ด์ฐฎ๋๋ณด๊ตฐ์!
} | ||
return dfs(index: 0, node: root) | ||
} | ||
} No newline at end of file |
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.
ํ์ผ ๋ฏธ์ง๋ง์ ์ค ๋ฐ๊ฟ์ด ๋๋ฝ์ด ๋์๋ค์. ์ฝ๋ ์์ฑํ์ค ๋ ์ด๋ฐ ์ฌ์ํ ๋ถ๋ถ์ ์ ๊ฒฝ ์ ์ฐ์์ง ์๋๋ก ํฌ๋ฉงํฐ ์ฌ์ฉ์ ์ถ์ฒ๋๋ฆฝ๋๋ค.
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.
Swift ํฌ๋งทํฐ๊ฐ ์์ด์ ์ด๊ฒ ์ฐธ ์ด๋ ต๋ค์ ๐ ์ฃผ์ํ๊ฒ ์ต๋๋ค.
211. Design Add and Search Words Data Structure
Complexities ๐
Explanation ๐
Trie๋ฅผ ๊ตฌ์ฑํ์ฌ ํ์ดํ์ต๋๋ค.$O(n)$ ๋ณด๋ค ์ข ๋ ์๊ฐ์ด ๊ฑธ๋ฆด ๊ฒ์
๋๋ค.
Trie๋ก ๊ฒ์ํ๋ ๊ตฌ๊ฐ์์
.
์ด ๋ค์ด์ค๋ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ํ์ ํด์ฃผ์ด์ผํ๊ธฐ ๋๋ฌธ์,์ด ๋ ์๊ฐ๋ณต์ก๋ ๊ณ์ฐ์ ์ด๋ป๊ฒ ํด์ผํ ์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค์ ๐ ํน์ ์๊ฒฌ์ด ์์ผ์๋ค๋ฉด ์ฒจ์ธ ๋ถํ๋๋ฆฌ๊ฒ ์ต๋๋ค.200. Number of Islands
Complexities ๐
Explanation ๐
N * M ๋งํผ ์ํํ๋ฉด์ "1"์ ์ฐพ๋ ์ฆ์ DFS๋ก ์ธ์ ํ ์ฌ์ ๋๋น๋ฅผ ๋ฐฉ๋ฌธ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๊ตฌํํ์ต๋๋ค.
133. Clone Graph
Complexities ๐
Explanation ๐
์๋ก์ด ๋ ธ๋๋ฅผ ์บ์ฑํ ๋์ ๋๋ฆฌ ํ์ ์ ๋ง๋ค๊ณ , ๊ธฐ์กด original Node๋ฅผ ๋๋ ค๊ฐ๋ฉฐ ์๋ก์ด ๋ณต์ฌ๋ณธ์ ์์ฑ ๋ฐ ์ฒ๋ฆฌํ์ต๋๋ค.
417. Pacific Atlantic Water Flow
Complexities ๐
Explanation ๐
207. Course Schedule (์์ง ํ์ด ์ ํจ)
Complexities ๐
Explanation ๐
์ฒ์์๋ ์ ๋์จ ํ์ธ๋๋ก ํ ์ ์๋ค๊ณ ์๊ฐํ์ผ๋ ์ํธ์ฐธ์กฐ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ๋ฐ๊ฒฌํ์ง ๋ชปํ์ฌ DFS๋ก ํ์ดํ์ต๋๋ค.