版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/87854214
399. Evaluate Division
class Solution(object):
def calcEquation(self, equations, values, queries):
graph = collections.defaultdict(set)
for i, vertices in enumerate(equations):
v1, v2 = vertices
value = values[i]
graph[v1].add((v2, value))
graph[v2].add((v1, 1/value))
def find_path(v1, v2):
if v1 not in graph or v2 not in graph:
return -1
q = []
q.append([v1, 1])
visited = set()
while q:
front, product = q.pop(0)
if front == v2:
return product
visited.add(front)
for neighbor, value in graph[front]:
if neighbor not in visited:
q.append([neighbor, product*value])
return -1
return [find_path(v1, v2) for v1, v2 in queries]
935. Knight Dialer
转换成可以有几条路径来跳N次
class Solution(object):
def knightDialer(self, N):
graph = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]]
memo = collections.defaultdict(dict)
MOD = 1000000007
def helper(n, curNum, memo): # 计算单个数字curNum 跳N次,有几条路径
if n == 0:
return 1
if memo[n].get(curNum) is not None:
return memo[n][curNum]
count = 0
for neig in graph[curNum]:
count += helper(n-1, neig, memo) % MOD
memo[n][curNum] = count
return count
res = 0
for i in xrange(10):
res += helper(N-1, i, memo) % MOD
return res % MOD
688. Knight Probability in Chessboard
class Solution(object):
def knightProbability(self, N, K, r, c):
memo = {}
moves = ((-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2))
# 每一步的概率都是上一步的概率除以8,累加所有路径可以走K步的
def dfs(k, x, y, P):
p = 0
if 0 <= x < N and 0 <= y < N:
if k < K:
for dx, dy in moves:
x_next, y_next = x+dx, y+dy
if (x_next, y_next, k+1) not in memo:
memo[(x_next, y_next, k+1)] = dfs(k+1, x_next, y_next, P/8)
p += memo[(x_next, y_next, k+1)]
else:
p = P
return p
return dfs(0, r, c, 1.0)
310. Minimum Height Trees
思路: 最短的MHT的root是最长的路径的中间节点。所以最多只会有2个root。计算所有点的入度,每次去掉入度为1的节点。对应的邻居就要入度-1.再次重复步骤去掉入度为1的节点,直到剩下小于2个点。
class Solution(object):
def findMinHeightTrees(self, n, edges):
if n == 1: return [0]
neighbors = collections.defaultdict(list)
degress = collections.defaultdict(int)
for u,v in edges:
neighbors[u].append(v)
neighbors[v].append(u)
degress[u] += 1
degress[v] += 1
level = []
for i in xrange(n): # 找到入度为1的
if degress[i] == 1:
level.append(i)
unvisited = set(range(n))
while len(unvisited) > 2: # 这里是unvisited >2 不是len(level)
nextLevel = []
for u in level:
unvisited.remove(u)
for neighbor in neighbors[u]:
if neighbor in unvisited:
degress[neighbor] -= 1
if degress[neighbor] == 1:
nextLevel.append(neighbor)
level = nextLevel
return level