记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
2/5 1696. 跳跃游戏 VI
从左到右记录最大值
堆来记录最大值及位置
def maxResult(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
import heapq
l = []
for i,num in enumerate(nums):
if l:
v,ind = l[0][0],l[0][1]
while ind<i-k :
heapq.heappop(l)
v,ind = l[0][0],l[0][1]
nums[i] = num-v
heapq.heappush(l,(-nums[i],i))
return nums[-1]
2/6 LCP 30. 魔塔游戏
如果总和小于0 说明不可以
记录当前遇到过的扣血房间从小到大排序
当遇到血不足时 将扣血最多的放到末尾
def magicTower(nums):
"""
:type nums: List[int]
:rtype: int
"""
import heapq
if sum(nums)<0:
return -1
l = []
cur = 1
ans = 0
for num in nums:
cur+=num
if num<0:
heapq.heappush(l,num)
if cur<1:
v = heapq.heappop(l)
cur-=v
ans +=1
return ans
2/7 2641. 二叉树的堂兄弟节点 II
BFS
记录节点以及父节点
统计当层节点总和以及上一层节点对应的子节点和
每一个节点更新为当前层数总和减去对应父节点的子节点和
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def replaceValueInTree(root):
"""
:type root: Optional[TreeNode]
:rtype: Optional[TreeNode]
"""
l = [(root,0)]
while l:
tmp = []
cnt = 0
total = 0
father = {}
for node,f in l:
if node.left:
tmp.append((node.left,cnt))
if node.right:
tmp.append((node.right,cnt))
cnt+=1
total += node.val
father[f] = father.get(f,0)+node.val
for node,f in l:
node.val = total - father[f]
l=tmp[:]
return root
2/8 993. 二叉树的堂兄弟节点
BFS
记录每个节点的父节点 如果在同一层且父节点不同则为堂兄弟
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def isCousins(root, x, y):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
if root.val==x or root.val==y:
return False
l = [root]
while l:
tmp = []
f = {}
for node in l:
if node.left:
f[node.left.val]=node.val
tmp.append(node.left)
if node.right:
f[node.right.val]=node.val
tmp.append(node.right)
if x in f and y in f:
if f.get(x,0)==f.get(y,0):
return True
else:
return False
elif x in f or y in f:
return False
l=tmp
return False
2/9 236. 二叉树的最近公共祖先
func返回p,q是否在当前节点
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def lowestCommonAncestor(root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
global ans
ans = None
def func(node):
global ans
if not node:
return False,False
l1,l2 = func(node.left)
r1,r2 = func(node.right)
ep = l1 or r1
eq = l2 or r2
if node==p:
ep = True
if node==q:
eq = True
if ep and eq and not ans:
ans = node
return ep,eq
func(root)
return ans
2/10 94. 二叉树的中序遍历
中序遍历 左根右
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def inorderTraversal(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack=[]
def traversal(node):
if node:
traversal(node.left)
stack.append(node.val)
traversal(node.right)
traversal(root)
return stack
2/11 144. 二叉树的前序遍历
前序遍历 根左右
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def inorderTraversal(root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack=[]
def traversal(node):
if node:
stack.append(node.val)
traversal(node.left)
traversal(node.right)
traversal(root)
return stack