[leetcode] 1443. Minimum Time to Collect All Apples in a Tree

Description

Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend in order to collect all apples in the tree starting at vertex 0 and coming back to this vertex.

The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting the vertices fromi and toi. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple, otherwise, it does not have any apple.

Example 1:
在这里插入图片描述

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
Output: 8 
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  

Example 2:
在这里插入图片描述

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
Output: 6
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows. 

Example 3:

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
Output: 0

Constraints:

  • 1 <= n <= 10^5
  • edges.length == n-1
  • edges[i].length == 2
  • 0 <= fromi, toi <= n-1
  • fromi < toi
  • hasApple.length == n

分析

题目的意思是:给你定一颗二叉树,求出遍历所有苹果的最小时间。这道题的二叉树是用边的形式给出来的,挺新颖的,当然我也没做出来。大概思路是要建立一个无向图,进行深度优先搜索,然后记录代价并求和就行了。注意无向图是有环的,不要陷入环里面去了,所以要比较一下当前结点的parent是否等于child。下面的代码中如果遍历的是非根结点

hasApple[parent] = True

主要是递归计算回传的时候记录中间经过的边,可以说是一个小trick了,我也想不到哈哈哈。

代码

class Solution:
    def solve(self,routes,hasApple,idx,parent):
        cost=0
        for child in routes[idx]:
            if(child!=parent):
                cost+=self.solve(routes,hasApple,child,idx)
        
        if(idx!=0 and hasApple[idx]):
            hasApple[parent]=True
            return cost+2
        return cost
        
    def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
        routes=collections.defaultdict(list)
        res=0
        for k,v in edges:
            routes[k].append(v)
            routes[v].append(k)
            
        res=self.solve(routes,hasApple,0,0)
        return res

参考文献

[LeetCode] [Python] Beats 100% Short DFS Easy to Read

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值