记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
dfs记录子树叶到根最大值
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sufficientSubset(root, limit):
"""
:type root: TreeNode
:type limit: int
:rtype: TreeNode
"""
def dfs(node,pre):
if node==None:
return False
if not node.left and not node.right:
return node.val+pre>=limit
left = dfs(node.left,pre+node.val)
right = dfs(node.right,pre+node.val)
if not left:
node.left = None
if not right:
node.right = None
return left or right
v = dfs(root,0)
return root if v else None
大顶堆 每次取最大value值
记录当前labal数量是否可用
def largestValsFromLabels(values, labels, numWanted, useLimit):
"""
:type values: List[int]
:type labels: List[int]
:type numWanted: int
:type useLimit: int
:rtype: int
"""
from collections import defaultdict
import heapq
l = [(-values[i],labels[i]) for i in range(len(values))]
heapq.heapify(l)
mem = defaultdict(int)
ans = 0
while numWanted>0 and len(l)>0:
v,lab = heapq.heappop(l)
v = -v
if mem[lab]<useLimit:
ans +=v
mem[lab]+=1
numWanted-=1
return ans
dfs 深搜
m[i]记录从i节点能够到达的后续节点
如果跳跃时间为0 或者后续无节点
判断当前节点是否为目标
nxt为后续可以选择的个数
def frogPosition(n, edges, t, target):
"""
:type n: int
:type edges: List[List[int]]
:type t: int
:type target: int
:rtype: float
"""
from collections import defaultdict
m = defaultdict(list)
for i,j in edges:
m[i].append(j)
m[j].append(i)
mem = [0]*(n+1)
def dfs(node,t):
nxt = len(m[node])
if node>1:
nxt -=1
if nxt==0 or t==0:
return 1 if node==target else 0
mem[node] = 1
for j in m[node]:
if not mem[j]:
p = dfs(j,t-1)
if p>0:
return p*1.0/nxt
return 0
return dfs(1,t)
遍历
def oddString(words):
"""
:type words: List[str]
:rtype: str
"""
def check(w):
diff = [0]*(len(w)-1)
for i in range(len(w)-1):
diff[i] = ord(w[i+1])-ord(w[i])
return diff
d0 = check(words[0])
d1 = check(words[1])
if d0==d1:
for i in range(2,len(words)):
if d0!=check(words[i]):
return words[i]
return words[0] if d1==check(words[2]) else words[1]
BFS 广搜
记录每个点是否经过
def shortestPathBinaryMatrix(grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
from collections import defaultdict
n=len(grid)
if grid[0][0]==1 or grid[n-1][n-1]==1:
return -1
m = defaultdict(int)
l = [(0,0)]
m[(0,0)]=1
step = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)]
while l:
tmp = []
for x,y in l:
for i,j in step:
nx,ny = x+i,y+j
if 0<=nx<n and 0<=ny<n and grid[nx][ny]==0:
if nx==n-1 and ny==n-1:
return m[(x,y)]+1
if m[(nx,ny)]==0 :
tmp.append((nx,ny))
m[(nx,ny)]=m[(x,y)]+1
l=tmp
return -1 if m[(n-1,n-1)]==0 else m[(n-1,n-1)]
分别统计计算
total = sum(count)
mean,median=0.0,0.0
maxv,minv = 0,256
fre = 0
mode = 0
l,r = (total+1)//2,(total+2)//2
su = 0
cnt = 0
for k in range(len(count)):
c = count[k]
su += k*c
if c>fre:
fre = c
mode = k
if c>0:
minv = min(minv,k)
maxv = k
if cnt<r and cnt+c>=r:
median+=k
if cnt<l and cnt+c>=l:
median+=k
cnt+=c
mean = su*1.0/total
median = median/2.0
return [minv,maxv,mean,median,mode]
每次只考虑两行的情况 求出前k个小的值
merge实现 f,g 最小堆保存f[0]+g[i] 选出当前最小的 f[j]+g[i] 将f[j+1]+g[i]加入
再与下一行得到前k个值 依次类推
def kthSmallest(mat, k):
"""
:type mat: List[List[int]]
:type k: int
:rtype: int
"""
import heapq
def merge(f,g,k):
if len(g)<len(f):
return merge(g,f,k)
l = [(f[0]+g[i],0,i) for i in range(len(g))]
heapq.heapify(l)
ans = []
while k and l:
v,indf,indg = heapq.heappop(l)
ans.append(v)
if indf+1<len(f):
heapq.heappush(l, (f[indf+1]+g[indg],indf+1,indg))
k-=1
return ans
pre = mat[0]
for i in range(1,len(mat)):
pre = merge(pre,mat[i],k)
return pre[k-1]