This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-08] dohyun #184 #213
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
|
||
풀이시간 | ||
- 약 1시간 30분 | ||
|
||
접근법 | ||
- 하나의 단어는 접근하면 다시 사용하지 않음 (왜? 다시 접근할거면 변환을 할 필요가 없으니까, 무한 루프도는거) | ||
- 한번 방문한 곳은 다시 안감 + 완전탐색 + 최소 count -> BFS | ||
- "한 번에 한 개의 알파벳만 바꿀 수 있다" 조건만 잘 처리하자! | ||
- 단어의 길이가 최대 10이므로 그냥 단순 순회하며 확인해보면 될 듯함 | ||
|
||
회고 | ||
- 한 개의 알파벳만 바꿀 수 있다 -> "BCB" 와 같은 단어가 오면 "ACA" 로도 바꿀 수 있다 | ||
- 이렇게 해석을 잘 못해서 시간이 좀 오래 걸렸음 ㅠ ㅠ | ||
|
||
""" | ||
|
||
from collections import deque | ||
|
||
def solution(begin, target, words): | ||
if target not in words: # 정답이 없으면 0 반환 | ||
return 0 | ||
|
||
def check_word(word1, word2): # 한 개의 알파벳만 바꿔도 되는지 여부를 반환 | ||
cnt = 0 | ||
for w1, w2 in zip(word1, word2): | ||
if w1 != w2: | ||
cnt += 1 | ||
if cnt > 1: | ||
return False | ||
return True | ||
|
||
words.append(begin) | ||
visit = [-1] * (len(words)) | ||
queue = deque([len(words)-1]) | ||
visit[len(words)-1] = 0 | ||
|
||
target_idx = words.index(target) | ||
|
||
while queue: # BFS | ||
node = queue.popleft() | ||
|
||
for i in range(len(words)): | ||
if visit[i] == -1: # 방문하지 않은 단어에만 접근 | ||
if check_word(words[node], words[i]): # 한개의 알파벳 조건 만족하면 | ||
queue.append(i) # 해당 단어도 큐에 추가 | ||
visit[i] = visit[node] + 1 | ||
elif i==target_idx: # 이미 방문한적이 있으며 target 에 도달했다면 정답 반환 | ||
return visit[target_idx] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
밑의 BFS를 인덱스를 활용해서 풀이하셨는데, 이 라인의 과정 없이 인덱스를 활용할 때 words 리스트를 인덱스로 참조해서 비교한는 방식으로 풀이하셨어도 가독성 측면에서 좋았을 것 같습니다.