小黑代码(小黑卡在了bug中,上午一步步探索做出,非常NB!!!)
# 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 rob(self, root: Optional[TreeNode]) -> int:# 递归函数进行动态规划def dfs(node):# 节点为空if not node:return 0, 0, 0# 叶子结点if not (node.left or node.right):return 0, 0, node.val# 动态规划操作first_1, second_1, node1_val = dfs(node.left)first_2, second_2, node2_val = dfs(node.right)third_1 = max(second_1, first_1+node1_val)third_2 = max(second_2, first_2+node2_val)return second_1+second_2, third_1+third_2, node.valfirst_root, second_root, val = dfs(root)return max(second_root, first_root+val)
动态规划法
# 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 rob(self, root: Optional[TreeNode]) -> int:# 递归函数# 返回[不选择该结点获得收益, 选择该结点获得的收益]def dfs(node):# 结点为空if not node:return [0, 0]# 获得左右结点的数组l = dfs(node.left)r = dfs(node.right)# 不选择该结点not_select = max(l[0], l[1]) + max(r[0], r[1])# 选择该结点select = node.val + l[0] + r[0]return [not_select, select]return max(dfs(root))
数据库练习
608. 树节点
使用union
# Write your MySQL query statement below
SELECTid, 'Root' AS 'type'
FROMTree
WHEREp_id IS NULLUNIONSELECTid, 'Leaf' AS 'type'
FROMTree
WHEREid NOT IN(SELECTp_idFROMTreeWHEREp_id IS NOT NULL)AND p_id IS NOT NULL
UNIONSELECTid, 'Inner' AS 'type'
FROMTree
WHEREid IN (SELECT DISTINCTp_idFROMTreeWHEREp_id IS NOT NULL)AND p_id IS NOT NULL
Case法(小黑第一次使用,了解了sql中的case的基本用法)
# Write your MySQL query statement below
SELECTid,CASEWHEN tree.p_id IS NULLTHEN 'Root'WHEN tree.id IN (SELECT DISTINCT p_id FROM Tree WHERE p_id IS NOT NULL) AND tree.p_id IS NOT NULLTHEN 'Inner'WHEN tree.id NOT IN (SELECT DISTINCT p_id FROM TREE WHERE p_id IS NOT NULL)THEN 'Leaf'ELSE 'other'END AS Type
FROMTree
IF分支法
# Write your MySQL query statement below
SELECTid,IF(ISNULL(Tree.p_id), 'Root', IF(Tree.id IN (SELECT DISTINCT p_id FROM Tree WHERE p_id IS NOT NULL) AND Tree.p_id IS NOT NULL, 'Inner', 'Leaf')) AS 'type'
FROMTree
小黑生活
边看店边学科一
看了看《强风吹拂》,来了点儿感觉
晚饭我弟弟买了肚
从我姥姥家回家
早上尝试五点起床跑个步,肚子比较难受
早餐混沌
早上同事给了我一块儿糖
买杯咖啡
早上忘记刮胡子了,外卖叫了个刮胡刀
午餐干饭
中午一起练歌,第一次进入部室馆
填写了干部履历表
学习深度强化学习的策略迭代部分
下班阳光照进了办公室
练完了琴来份烤冷面
我的快递到了,能量胶和正山小种茶
还是查不到社保公积金数据
早上继续五点起来跑步,天上下雨了,终于意识到以后不空腹吃能量胶了,感觉对胃的刺激还是比较大的
早上把茶带到了单位,正山小种
继续学习强化学习
午餐干饭,今天又打多了,剩了一点儿,明天一定少打点儿好好养养胃
练完歌在咖啡厅里买杯冰镇柚子茶聊聊天