- ์ฒ์์ ํ ์คํธ ์ผ์ด์ค๋ง ๋ณด๊ณ ๋์ด๋ธํ๊ฒ ์๊ฐํ๋ค.
- ์ด์ฐจํผ ์ต์ ๋จ๊ณ๋ (๊ธ์์ + 1) ์๋๋... len(begin)+1 ๋ฆฌํดํ๋ฉด ๋์ง?
- ๊ทธ ๊ฒฐ๊ณผ๋ ๋น์ฐํ ์คํจ์ง ใ ใ ใ
- ์์ฒญ ๊ผฌ์๋์ ๊ฒ์ด ๋ถ๋ช ํ๋ค.
- hit -> hot -> hom -> mom -> mow -> cow -> cog ์ด๋ฐ ์...
- ๊ทธ๋ฆฌ๊ณ ์ฒ์์ ๋ณํํ ์ ์๋ ๋จ์ด๋ ์ฌ๋ฌ ๊ฐ์ผ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
- hit -> hot, hin, cit ์ด๋ ๊ฒ ๋ค ๋ฐ๊ฟ ์๋ ์์๊ฑฐ๊ณ ...
- ๊ฒฐ๊ตญ ์ถ๋ฐ์ง๋ ์ฌ๋ฌ ๊ฐ๊ณ ๋์ฐฉ์ง๋ ํ๋์ธ ์.
- ๊ทธ๋ฆฌ๊ณ ๊ทธ ์ค์ ๊ฐ์ฅ ์งง์ ๊ฒฝ๋ก๋ฅผ ๊ณ ๋ฅด๋ ๋ฌธ์ .
๋ฌธ์ ์ค๋ช
๋ ๊ฐ์ ๋จ์ด begin, target๊ณผ ๋จ์ด์ ์งํฉ words๊ฐ ์์ต๋๋ค. ์๋์ ๊ฐ์ ๊ท์น์ ์ด์ฉํ์ฌ begin์์ target์ผ๋ก ๋ณํํ๋ ๊ฐ์ฅ ์งง์ ๋ณํ ๊ณผ์ ์ ์ฐพ์ผ๋ ค๊ณ ํฉ๋๋ค.
1. ํ ๋ฒ์ ํ ๊ฐ์ ์ํ๋ฒณ๋ง ๋ฐ๊ฟ ์ ์์ต๋๋ค.
2. words์ ์๋ ๋จ์ด๋ก๋ง ๋ณํํ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด begin์ด "hit", target๊ฐ "cog", words๊ฐ ["hot","dot","dog","lot","log","cog"]๋ผ๋ฉด "hit" -> "hot" -> "dot" -> "dog" -> "cog"์ ๊ฐ์ด 4๋จ๊ณ๋ฅผ ๊ฑฐ์ณ ๋ณํํ ์ ์์ต๋๋ค.
๋ ๊ฐ์ ๋จ์ด begin, target๊ณผ ๋จ์ด์ ์งํฉ words๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ์ต์ ๋ช ๋จ๊ณ์ ๊ณผ์ ์ ๊ฑฐ์ณ begin์ target์ผ๋ก ๋ณํํ ์ ์๋์ง return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ ์ฌํญ
- ๊ฐ ๋จ์ด๋ ์ํ๋ฒณ ์๋ฌธ์๋ก๋ง ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- ๊ฐ ๋จ์ด์ ๊ธธ์ด๋ 3 ์ด์ 10 ์ดํ์ด๋ฉฐ ๋ชจ๋ ๋จ์ด์ ๊ธธ์ด๋ ๊ฐ์ต๋๋ค.
- words์๋ 3๊ฐ ์ด์ 50๊ฐ ์ดํ์ ๋จ์ด๊ฐ ์์ผ๋ฉฐ ์ค๋ณต๋๋ ๋จ์ด๋ ์์ต๋๋ค.
- begin๊ณผ target์ ๊ฐ์ง ์์ต๋๋ค.
- ๋ณํํ ์ ์๋ ๊ฒฝ์ฐ์๋ 0๋ฅผ return ํฉ๋๋ค.
์ ์ถ๋ ฅ ์
begin | target | words | return |
"hit" | "cog" | ["hot", "dot", "dog", "lot", "log", "cog"] | 4 |
"hit" | "cog" | ["hot", "dot", "dog", "lot", "log"] | 0 |
์ ์ถ๋ ฅ ์ ์ค๋ช
์์ #1
๋ฌธ์ ์ ๋์จ ์์ ๊ฐ์ต๋๋ค.
์์ #2
target์ธ "cog"๋ words ์์ ์๊ธฐ ๋๋ฌธ์ ๋ณํํ ์ ์์ต๋๋ค.
ํ์ด
- ๋ฌธ์ ๊ฐ DFS๋ BFS ์ฐ๋ ๋ฌธ์ ๋ผ๊ณ ํ์ผ๋... ๋ค์ ์ ๋๋ก ์๊ฐํด๋ณด๊ธฐ...
- ๊ท์ฐฎ์ผ๋ ๊ทธ๋ฅ ๋์ ๋๋ฆฌ์ ๋ฃ๊ณ for ๋ฌธ ๋๋ ค... ์ ์คํจ๋์ง?
def solution(begin, target, words):
answer = 0
if target not in words:
return answer
words_bridge = {}
words_bridge[begin] = 0
for word in words: words_bridge[word] = 999
for depth in range(len(words)):
print(words_bridge)
for key, value in words_bridge.items():
if value == depth:
for word in words:
if len(set(list(word)) & set(list(key))) >= 2:
if words_bridge[word] > depth:
words_bridge[word] = depth + 1
if word == target:
return words_bridge[target]
print(words_bridge)
return words_bridge[target]
ํ
์คํธ 1
์
๋ ฅ๊ฐ ใ "hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]
๊ธฐ๋๊ฐ ใ 4
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
์ถ๋ ฅ ใ
{'hit': 0, 'hot': 999, 'dot': 999, 'dog': 999, 'lot': 999, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 999, 'dog': 999, 'lot': 999, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 2, 'dog': 999, 'lot': 2, 'log': 999, 'cog': 999}
{'hit': 0, 'hot': 1, 'dot': 2, 'dog': 3, 'lot': 2, 'log': 3, 'cog': 999}
ํ
์คํธ 2
์
๋ ฅ๊ฐ ใ "hit", "cog", ["hot", "dot", "dog", "lot", "log"]
๊ธฐ๋๊ฐ ใ 0
์คํ ๊ฒฐ๊ณผ ใ ํ
์คํธ๋ฅผ ํต๊ณผํ์์ต๋๋ค.
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ์คํจ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.26ms, 10.3MB)
ํ
์คํธ 3 ใ ์คํจ (0.02ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.00ms, 10.2MB)
- ํ์ฐธ ์๊ฐํด๋ณด๋
- ์... ํ์ํ ๊ธ์๊ฐ ์๋๊ฒ ์๋๋ผ... ๊ธ์ ์์๋ ๋์ผํด์ผ ํ๋๊ตฌ๋!
- ์ฝ๊ฒ ๊ฐ๋ ค๊ณ ํ๋ ์ด ์ฝ๋๊ฐ ๋ฐ๋ชฉ์ ์ก์๋ค. ๊ดํ ์๊ฐ ๋ญ๋น ํ๋ค.
- if len(set(list(word)) & set(list(key))) >= 2:
- ์... ํ์ํ ๊ธ์๊ฐ ์๋๊ฒ ์๋๋ผ... ๊ธ์ ์์๋ ๋์ผํด์ผ ํ๋๊ตฌ๋!
def solution(begin, target, words):
answer = 0
if target not in words:
return answer
words_bridge = {}
for word in words: words_bridge[word] = 999
words_bridge[begin] = 0
i_will_leave_here = False
for depth in range(len(words)):
if i_will_leave_here == True:
break
for key, value in words_bridge.items():
if value == depth:
for word in words:
diffrent_character = 0
for i in range(len(word)):
if list(word)[i] != list(key)[i]:
diffrent_character += 1
if diffrent_character == 1:
if word == target:
i_will_leave_here = True
if words_bridge[word] > depth + 1:
words_bridge[word] = depth + 1
#print(words_bridge)
return words_bridge[target]
- ๊ทธ๊ฑฐ ๋ง๊ณ ๋ ํ๋ฆฐ ๋ถ๋ถ ์์๋๋ฐ ใ ใ ใ
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.16ms, 10.1MB)
ํ
์คํธ 3 ใ ํต๊ณผ (1.64ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.00ms, 10MB)
- ๊ณ ์์ ํ์ด
from collections import deque
def get_adjacent(current, words):
for word in words:
if len(current) != len(word):
continue
count = 0
for c, w in zip(current, word):
if c != w:
count += 1
if count == 1:
yield word
def solution(begin, target, words):
dist = {begin: 0}
queue = deque([begin])
while queue:
current = queue.popleft()
for next_word in get_adjacent(current, words):
if next_word not in dist:
dist[next_word] = dist[current] + 1
queue.append(next_word)
return dist.get(target, 0)
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.07ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.81ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.1MB)
- ์์ฐ? ์ด๊ฑด ๋ญ์ง?
def solution(begin, target, words):
answer = 0
Q = [begin]
while True:
temp_Q = []
for word_1 in Q:
if word_1 == target:
return answer
for i in range(len(words)-1, -1, -1):
word_2 = words[i]
if sum([x!=y for x, y in zip(word_1, word_2)]) == 1:
temp_Q.append(words.pop(i))
if not temp_Q:
return 0
Q = temp_Q
answer += 1
ํ
์คํธ 1 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.11ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.53ms, 10.1MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.01ms, 10.2MB)
- ์์๊ฒ ๋ค๋ฌ ๋ค๋ฌ ใ ใ ใ
def solution(begin, target, words):
answer = 0
if target not in words:
return answer
words_bridge = {}
for word in words: words_bridge[word] = 999
words_bridge[begin] = 0
for depth in range(len(words)):
for key, value in words_bridge.items():
if value == depth:
for word in words:
if sum(1 for a,b in zip(word,key) if a != b) == 1:
if words_bridge[word] > depth + 1:
words_bridge[word] = depth + 1
if word == target: break
return words_bridge[target]
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.12ms, 10.4MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.53ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.00ms, 10.3MB)
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ์ผ๊ทผ์ง์ (0) | 2023.02.25 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ์ฌํ๊ฒฝ๋ก - DFS ๋ฌธ์ (1) | 2023.02.25 |
ํ๋ก๊ทธ๋๋จธ์ค ๋คํธ์ํฌ - ๊น์ด/๋๋น ์ฐ์ ํ์(DFS/BFS) (0) | 2023.02.24 |
ํ๋ก๊ทธ๋๋จธ์ค ํผ์์ ํ๋ ํฑํํ (2) | 2023.02.24 |
ํ๋ก๊ทธ๋๋จธ์ค ๋์ถฉ ๋ง๋ ์ํ (0) | 2023.02.23 |