forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e016230
commit 4e68c24
Showing
7 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
public: | ||
int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) { | ||
for (auto& it : edges) { | ||
int i = it[0], j = it[1]; | ||
g[i].emplace_back(j); | ||
g[j].emplace_back(i); | ||
} | ||
|
||
int vis[n]; | ||
memset(vis, 0, sizeof vis); | ||
vis[0] = 1; | ||
return dfs(0, hasApple, vis); | ||
} | ||
private: | ||
unordered_map<int, vector<int>> g; | ||
|
||
int dfs(int x, vector<bool>& hasApple, int vis[]) { | ||
int res = 0; | ||
for (int i : g[x]) { | ||
if (vis[i]) continue; | ||
vis[i] = 1; | ||
|
||
int cur = dfs(i, hasApple, vis); | ||
if (cur > 0) res += cur + 2; | ||
else if (hasApple[i]) res += 2; | ||
} | ||
return res; | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var g map[int][]int | ||
|
||
func minTime(n int, edges [][]int, hasApple []bool) int { | ||
g = make(map[int][]int) | ||
for _, edge := range edges { | ||
i, j := edge[0], edge[1] | ||
g[i] = append(g[i], j) | ||
g[j] = append(g[j], i) | ||
} | ||
|
||
vis := make([]bool, n) | ||
vis[0] = true | ||
return dfs(0, hasApple, vis) | ||
} | ||
|
||
func dfs(x int, hasApple, vis []bool) int { | ||
res := 0 | ||
for _, i := range g[x] { | ||
if vis[i] { | ||
continue | ||
} | ||
vis[i] = true | ||
|
||
cur := dfs(i, hasApple, vis) | ||
if cur > 0 { | ||
res += cur + 2 | ||
} else if hasApple[i] { | ||
res += 2 | ||
} | ||
} | ||
return res | ||
} |
30 changes: 30 additions & 0 deletions
30
src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
public int minTime(int n, int[][] edges, List<Boolean> hasApple) { | ||
for (int[] it : edges) { | ||
int i = it[0], j = it[1]; | ||
g.putIfAbsent(i, new ArrayList()); | ||
g.putIfAbsent(j, new ArrayList()); | ||
g.get(i).add(j); | ||
g.get(j).add(i); | ||
} | ||
|
||
boolean[] vis = new boolean[n]; | ||
vis[0] = true; | ||
return dfs(0, hasApple, vis); | ||
} | ||
|
||
private Map<Integer, List<Integer>> g = new HashMap(); | ||
|
||
private int dfs(int x, List<Boolean> hasApple, boolean[] vis) { | ||
int res = 0; | ||
for (int i : g.get(x)) { | ||
if (vis[i]) continue; | ||
vis[i] = true; | ||
|
||
int cur = dfs(i, hasApple, vis); | ||
if (cur > 0) res += cur + 2; | ||
else if (hasApple.get(i)) res += 2; | ||
} | ||
return res; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var minTime = function(n, edges, hasApple) { | ||
let g = {}; | ||
for (let edge of edges) { | ||
let i = edge[0], j = edge[1]; | ||
g[i] = g[i] || []; | ||
g[j] = g[j] || []; | ||
g[i].push(j); | ||
g[j].push(i); | ||
} | ||
|
||
let vis = Array(n).fill(0); | ||
vis[0] = 1; | ||
let dfs = function(x) { | ||
let res = 0; | ||
for (let i of g[x]) { | ||
if (vis[i]) continue; | ||
vis[i] = 1; | ||
|
||
let cur = dfs(i); | ||
if (cur) res += cur + 2; | ||
else if (hasApple[i]) res += 2; | ||
} | ||
return res; | ||
} | ||
return dfs(0); | ||
}; |
21 changes: 21 additions & 0 deletions
21
src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int: | ||
g = collections.defaultdict(list) | ||
for i, j in edges: | ||
g[i].append(j) | ||
g[j].append(i) | ||
|
||
vis = [False] * n | ||
vis[0] = True | ||
def dfs(x): | ||
res = 0 | ||
for i in g[x]: | ||
if vis[i]: continue | ||
vis[i] = True | ||
|
||
cur = dfs(i) | ||
if cur: res += cur + 2 | ||
elif hasApple[i]: res += 2 | ||
|
||
return res | ||
return dfs(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters