Skip to content

Commit 65d26a5

Browse files
committed
feat: add solutions to lc problem: No.1443
No.1443.Minimum Time to Collect All Apples in a Tree
1 parent 54e6c13 commit 65d26a5

File tree

6 files changed

+313
-2
lines changed

6 files changed

+313
-2
lines changed

solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,129 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:DFS**
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
67+
def dfs(u, cost):
68+
if vis[u]:
69+
return 0
70+
vis[u] = True
71+
nxt_cost = 0
72+
for v in g[u]:
73+
nxt_cost += dfs(v, 2)
74+
if not hasApple[u] and nxt_cost == 0:
75+
return 0
76+
return cost + nxt_cost
77+
78+
g = defaultdict(list)
79+
for u, v in edges:
80+
g[u].append(v)
81+
g[v].append(u)
82+
vis = [False] * n
83+
return dfs(0, 0)
6484
```
6585

6686
### **Java**
6787

6888
<!-- 这里可写当前语言的特殊实现逻辑 -->
6989

7090
```java
91+
class Solution {
92+
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
93+
boolean[] vis = new boolean[n];
94+
List<Integer>[] g = new List[n];
95+
for (int i = 0; i < n; ++i) {
96+
g[i] = new ArrayList<>();
97+
}
98+
for (int[] e : edges) {
99+
int u = e[0], v = e[1];
100+
g[u].add(v);
101+
g[v].add(u);
102+
}
103+
return dfs(0, 0, g, hasApple, vis);
104+
}
105+
106+
private int dfs(int u, int cost, List<Integer>[] g, List<Boolean> hasApple, boolean[] vis) {
107+
if (vis[u]) {
108+
return 0;
109+
}
110+
vis[u] = true;
111+
int nxtCost = 0;
112+
for (int v : g[u]) {
113+
nxtCost += dfs(v, 2, g, hasApple, vis);
114+
}
115+
if (!hasApple.get(u) && nxtCost == 0) {
116+
return 0;
117+
}
118+
return cost + nxtCost;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
129+
vector<bool> vis(n);
130+
vector<vector<int>> g(n);
131+
for (auto& e : edges)
132+
{
133+
int u = e[0], v = e[1];
134+
g[u].push_back(v);
135+
g[v].push_back(u);
136+
}
137+
return dfs(0, 0, g, hasApple, vis);
138+
}
139+
140+
int dfs(int u, int cost, vector<vector<int>>& g, vector<bool>& hasApple, vector<bool>& vis) {
141+
if (vis[u]) return 0;
142+
vis[u] = true;
143+
int nxt = 0;
144+
for (int& v : g[u]) nxt += dfs(v, 2, g, hasApple, vis);
145+
if (!hasApple[u] && !nxt) return 0;
146+
return cost + nxt;
147+
}
148+
};
149+
```
71150

151+
### **Go**
152+
153+
```go
154+
func minTime(n int, edges [][]int, hasApple []bool) int {
155+
vis := make([]bool, n)
156+
g := make([][]int, n)
157+
for _, e := range edges {
158+
u, v := e[0], e[1]
159+
g[u] = append(g[u], v)
160+
g[v] = append(g[v], u)
161+
}
162+
var dfs func(int, int) int
163+
dfs = func(u, cost int) int {
164+
if vis[u] {
165+
return 0
166+
}
167+
vis[u] = true
168+
nxt := 0
169+
for _, v := range g[u] {
170+
nxt += dfs(v, 2)
171+
}
172+
if !hasApple[u] && nxt == 0 {
173+
return 0
174+
}
175+
return cost + nxt
176+
}
177+
return dfs(0, 0)
178+
}
72179
```
73180

74181
### **...**

solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/README_EN.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,125 @@
4646

4747
## Solutions
4848

49+
DFS.
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
```python
54-
56+
class Solution:
57+
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
58+
def dfs(u, cost):
59+
if vis[u]:
60+
return 0
61+
vis[u] = True
62+
nxt_cost = 0
63+
for v in g[u]:
64+
nxt_cost += dfs(v, 2)
65+
if not hasApple[u] and nxt_cost == 0:
66+
return 0
67+
return cost + nxt_cost
68+
69+
g = defaultdict(list)
70+
for u, v in edges:
71+
g[u].append(v)
72+
g[v].append(u)
73+
vis = [False] * n
74+
return dfs(0, 0)
5575
```
5676

5777
### **Java**
5878

5979
```java
80+
class Solution {
81+
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
82+
boolean[] vis = new boolean[n];
83+
List<Integer>[] g = new List[n];
84+
for (int i = 0; i < n; ++i) {
85+
g[i] = new ArrayList<>();
86+
}
87+
for (int[] e : edges) {
88+
int u = e[0], v = e[1];
89+
g[u].add(v);
90+
g[v].add(u);
91+
}
92+
return dfs(0, 0, g, hasApple, vis);
93+
}
94+
95+
private int dfs(int u, int cost, List<Integer>[] g, List<Boolean> hasApple, boolean[] vis) {
96+
if (vis[u]) {
97+
return 0;
98+
}
99+
vis[u] = true;
100+
int nxtCost = 0;
101+
for (int v : g[u]) {
102+
nxtCost += dfs(v, 2, g, hasApple, vis);
103+
}
104+
if (!hasApple.get(u) && nxtCost == 0) {
105+
return 0;
106+
}
107+
return cost + nxtCost;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
118+
vector<bool> vis(n);
119+
vector<vector<int>> g(n);
120+
for (auto& e : edges)
121+
{
122+
int u = e[0], v = e[1];
123+
g[u].push_back(v);
124+
g[v].push_back(u);
125+
}
126+
return dfs(0, 0, g, hasApple, vis);
127+
}
128+
129+
int dfs(int u, int cost, vector<vector<int>>& g, vector<bool>& hasApple, vector<bool>& vis) {
130+
if (vis[u]) return 0;
131+
vis[u] = true;
132+
int nxt = 0;
133+
for (int& v : g[u]) nxt += dfs(v, 2, g, hasApple, vis);
134+
if (!hasApple[u] && !nxt) return 0;
135+
return cost + nxt;
136+
}
137+
};
138+
```
60139

140+
### **Go**
141+
142+
```go
143+
func minTime(n int, edges [][]int, hasApple []bool) int {
144+
vis := make([]bool, n)
145+
g := make([][]int, n)
146+
for _, e := range edges {
147+
u, v := e[0], e[1]
148+
g[u] = append(g[u], v)
149+
g[v] = append(g[v], u)
150+
}
151+
var dfs func(int, int) int
152+
dfs = func(u, cost int) int {
153+
if vis[u] {
154+
return 0
155+
}
156+
vis[u] = true
157+
nxt := 0
158+
for _, v := range g[u] {
159+
nxt += dfs(v, 2)
160+
}
161+
if !hasApple[u] && nxt == 0 {
162+
return 0
163+
}
164+
return cost + nxt
165+
}
166+
return dfs(0, 0)
167+
}
61168
```
62169

63170
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
4+
vector<bool> vis(n);
5+
vector<vector<int>> g(n);
6+
for (auto& e : edges)
7+
{
8+
int u = e[0], v = e[1];
9+
g[u].push_back(v);
10+
g[v].push_back(u);
11+
}
12+
return dfs(0, 0, g, hasApple, vis);
13+
}
14+
15+
int dfs(int u, int cost, vector<vector<int>>& g, vector<bool>& hasApple, vector<bool>& vis) {
16+
if (vis[u]) return 0;
17+
vis[u] = true;
18+
int nxt = 0;
19+
for (int& v : g[u]) nxt += dfs(v, 2, g, hasApple, vis);
20+
if (!hasApple[u] && !nxt) return 0;
21+
return cost + nxt;
22+
}
23+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func minTime(n int, edges [][]int, hasApple []bool) int {
2+
vis := make([]bool, n)
3+
g := make([][]int, n)
4+
for _, e := range edges {
5+
u, v := e[0], e[1]
6+
g[u] = append(g[u], v)
7+
g[v] = append(g[v], u)
8+
}
9+
var dfs func(int, int) int
10+
dfs = func(u, cost int) int {
11+
if vis[u] {
12+
return 0
13+
}
14+
vis[u] = true
15+
nxt := 0
16+
for _, v := range g[u] {
17+
nxt += dfs(v, 2)
18+
}
19+
if !hasApple[u] && nxt == 0 {
20+
return 0
21+
}
22+
return cost + nxt
23+
}
24+
return dfs(0, 0)
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
3+
boolean[] vis = new boolean[n];
4+
List<Integer>[] g = new List[n];
5+
for (int i = 0; i < n; ++i) {
6+
g[i] = new ArrayList<>();
7+
}
8+
for (int[] e : edges) {
9+
int u = e[0], v = e[1];
10+
g[u].add(v);
11+
g[v].add(u);
12+
}
13+
return dfs(0, 0, g, hasApple, vis);
14+
}
15+
16+
private int dfs(int u, int cost, List<Integer>[] g, List<Boolean> hasApple, boolean[] vis) {
17+
if (vis[u]) {
18+
return 0;
19+
}
20+
vis[u] = true;
21+
int nxtCost = 0;
22+
for (int v : g[u]) {
23+
nxtCost += dfs(v, 2, g, hasApple, vis);
24+
}
25+
if (!hasApple.get(u) && nxtCost == 0) {
26+
return 0;
27+
}
28+
return cost + nxtCost;
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int:
3+
def dfs(u, cost):
4+
if vis[u]:
5+
return 0
6+
vis[u] = True
7+
nxt_cost = 0
8+
for v in g[u]:
9+
nxt_cost += dfs(v, 2)
10+
if not hasApple[u] and nxt_cost == 0:
11+
return 0
12+
return cost + nxt_cost
13+
14+
g = defaultdict(list)
15+
for u, v in edges:
16+
g[u].append(v)
17+
g[v].append(u)
18+
vis = [False] * n
19+
return dfs(0, 0)

0 commit comments

Comments
 (0)