Day36力扣打卡

打卡记录

T 秒后青蛙的位置(DFS)

链接

python 复制代码
class Solution:
    def frogPosition(self, n: int, edges: List[List[int]], t: int, target: int) -> float:
        g = [[] for _ in range(n + 1)]
        for x, y in edges:
            g[x].append(y)
            g[y].append(x)
        g[1].append(0)
        ans = 0
        def dfs(x, fa, time, prod):
            if x == target and (time == 0 or len(g[x]) == 1):
                nonlocal ans
                ans = 1 / prod
                return True
            if x == target or time == 0: return False
            for y in g[x]:
                if y == fa: continue
                if dfs(y, x, time - 1, prod * (len(g[x]) - 1)): return True
            return False
        dfs(1, 0, t, 1)
        return ans

树上最大得分和路径(DFS)

链接

python 复制代码
class Solution:
    def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
        n = len(amount)
        g = [[] for _ in range(n)]
        for x, y in edges:
            g[x].append(y)
            g[y].append(x)
        g[0].append(-1)
        bob_time = [n] * n
        def dfs_bob(x: int, fa: int, t: int) -> bool:
            if x == 0:
                bob_time[x] = t
                return True
            for y in g[x]:
                if y != fa and dfs_bob(y, x, t + 1):
                    bob_time[x] = t
                    return True
            return False
        dfs_bob(bob, -1, 0)

        ans = -inf
        def dfs_alice(x: int, fa: int, alice_time: int, tot: int) -> None:
            if alice_time < bob_time[x]:
                tot += amount[x]
            elif alice_time == bob_time[x]:
                tot += amount[x] // 2
            if len(g[x]) == 1:
                nonlocal ans
                ans = max(ans, tot)
                return
            for y in g[x]:
                if y != fa:
                    dfs_alice(y, x, alice_time + 1, tot)
        dfs_alice(0, -1, 0, 0)
        return ans
相关推荐
天天进步20157 小时前
Python全栈项目--基于计算机视觉的车牌识别系统
开发语言·python·计算机视觉
大数据张老师7 小时前
数据结构——折半查找
数据结构·算法·查找·折半查找
软件开发技术深度爱好者7 小时前
使用Python实现播放“.gif”文件增强版
开发语言·python
熬了夜的程序员8 小时前
【LeetCode】87. 扰乱字符串
算法·leetcode·职场和发展·排序算法
感哥8 小时前
Django Model高级特性
python·django
李辉20038 小时前
Python简介及Pycharm
开发语言·python·pycharm
赵谨言8 小时前
基于python大数据的城市扬尘数宇化监控系统的设计与开发
大数据·开发语言·经验分享·python
是码农一枚8 小时前
全域感知,主动预警:视频汇聚平台EasyCVR打造水库大坝智慧安防视频监控智能分析方案
算法
MicroTech20258 小时前
微算法科技(NASDAQ MLGO)探索自适应差分隐私机制(如AdaDP),根据任务复杂度动态调整噪声
人工智能·科技·算法
云和数据.ChenGuang8 小时前
parser_error UnicodeDecodeError: ‘utf-8‘ codec can‘t decode bytes
python