파비의 매일매일 공부기록

Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.11.04
Today's Challenge  (0) 2022.11.03
Today's Challenge  (0) 2022.11.01
Today's Challenge  (0) 2022.10.31
Today's Challenge  (0) 2022.10.30
Comments