From 42b7d235c5bbf2090c184514c64f2d51d3fe8e2e Mon Sep 17 00:00:00 2001 From: Nathan Rihet <50133907+NathanKneT@users.noreply.github.com> Date: Sat, 2 Aug 2025 23:51:29 +0900 Subject: [PATCH] add 1443-minimum-time-to-collect-all-apples-in-a-tree.py --- ...um-time-to-collect-all-apples-in-a-tree.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 python/kotlin/1443-minimum-time-to-collect-all-apples-in-a-tree.py diff --git a/python/kotlin/1443-minimum-time-to-collect-all-apples-in-a-tree.py b/python/kotlin/1443-minimum-time-to-collect-all-apples-in-a-tree.py new file mode 100644 index 000000000..a5495f0e9 --- /dev/null +++ b/python/kotlin/1443-minimum-time-to-collect-all-apples-in-a-tree.py @@ -0,0 +1,21 @@ +from typing import List +from collections import defaultdict + +class Solution: + def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int: + graph = defaultdict(list) + for u, v in edges: + graph[u].append(v) + graph[v].append(u) + + def dfs(node: int, parent: int) -> int: + total_time = 0 + for child in graph[node]: + if child == parent: + continue + time_from_child = dfs(child, node) + if time_from_child > 0 or hasApple[child]: + total_time += time_from_child + 2 + return total_time + + return dfs(0, -1) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy