121、买股票的最佳时机
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_p, max_p = 999999, 0
for i in range(len(prices)):
min_p = min(min_p, prices[i])
max_p = max(max_p, prices[i] - min_p)
return max_p
122、买卖股票的最佳时机2
class Solution:
def maxProfit(self, prices: List[int]) -> int:
return sum(filter(lambda x:x>0, map(lambda x:prices[x+1]-prices[x], range(len(prices)-1))))
124、二叉树中的最大路径和
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
dpl = defaultdict(int)
dpr = defaultdict(int)
dplr = defaultdict(int)
if not root:return 0
mx = root.val
def dfs(root):
nonlocal mx
if not root:return
dfs(root.left)
dfs(root.right)
dpl[root] = max(dpl[root.left]+root.val,dpr[root.left]+root.val,root.val)
dpr[root] = max(dpl[root.right]+root.val,dpr[root.right]+root.val,root.val)
dplr[root] = dpl[root]+dpr[root]-root.val
mx = max(mx,dpl[root],dpr[root],dplr[root])
dfs(root)
return mx