-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (64 loc) · 2.4 KB
/
main.py
File metadata and controls
73 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Source: https://leetcode.com/problems/minimum-genetic-mutation
# Title: Minimum Genetic Mutation
# Difficulty: Medium
# Author: Mu Yang <http://muyang.pro>
################################################################################################################################
# Given the `root` of a binary search tree, and an integer `k`, return the `k^th` smallest value (**1-indexed**) of all the values of the nodes in the tree.
#
# **Example 1:**
# https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg
#
# ```
# Input: root = [3,1,4,null,2], k = 1
# Output: 1
# ```
#
# **Example 2:**
# https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg
#
# ```
# Input: root = [5,3,6,2,4,null,null,1], k = 3
# Output: 3
# ```
#
# **Constraints:**
#
# - The number of nodes in the tree is `n`.
# - `1 <= k <= n <= 10^4`
# - `0 <= Node.val <= 10^4`
#
# **Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
#
################################################################################################################################
from collections import defaultdict, deque
from typing import List
# Create Graph
class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
def diff(str1: str, str2: str) -> int:
return sum((int(ch1 != ch2) for ch1, ch2 in zip(str1, str2)))
# Prepare graph
graph: defaultdict[str, list[str]] = defaultdict(list)
for gene in bank:
if diff(startGene, gene) == 1:
graph[startGene].append(gene)
graph[gene].append(startGene)
for i, gene1 in enumerate(bank):
for gene2 in bank[:i]:
if diff(gene1, gene2) == 1:
graph[gene1].append(gene2)
graph[gene2].append(gene1)
# BFS
visited: defaultdict[str, bool] = defaultdict(bool)
queue: deque[tuple[str, int]] = deque() # node, dist
queue.append((startGene, 0))
visited[startGene] = True
while queue:
head, dist = queue.popleft()
for gene in graph[head]:
if gene == endGene:
return dist + 1
if not visited[gene]:
visited[gene] = True
queue.append((gene, dist + 1))
return -1