Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 11. 2. 23:45
https://leetcode.com/problems/minimum-genetic-mutation/
Minimum Genetic Mutation - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오늘도 BFS 변형 문제.
근데 솔루션 보고 푼 뒤에도 문제를 잘 이해 못하게씀..
class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
qu = deque([(start, 0)])
seen = {start}
while qu:
node, steps = qu.popleft()
if node == end:
return steps
for c in 'ACGT':
for i in range(len(node)):
neighbor = node[:i] + c + node[i + 1:]
if neighbor not in seen and neighbor in bank:
qu.append((neighbor, steps + 1))
seen.add(neighbor)
return -1
반응형