diff --git a/README.md b/README.md index 69795a4e..be8044e8 100644 --- a/README.md +++ b/README.md @@ -729,4 +729,5 @@ LeetCode |1438|[Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)|c|[c++](./src/1438-Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.cpp)|[python](./src/1438-Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.py)|[go](./src/1438-Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.go)|[js](./src/1438-Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.js)|[java](./src/1438-Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit/1438.java)|Medium| |1439|[Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/)|c|[c++](./src/1439-Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439.cpp)|[python](./src/1439-Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439.py)|[go](./src/1439-Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439.go)|[js](./src/1439-Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439.js)|[java](./src/1439-Find-the-Kth-Smallest-Sum-of-a-Matrix-With-Sorted-Rows/1439.java)|Hard| |1441|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|c|[c++](./src/1441-Build-an-Array-With-Stack-Operations/1441.cpp)|[python](./src/1441-Build-an-Array-With-Stack-Operations/1441.py)|[go](./src/1441-Build-an-Array-With-Stack-Operations/1441.go)|[js](./src/1441-Build-an-Array-With-Stack-Operations/1441.js)|[java](./src/1441-Build-an-Array-With-Stack-Operations/1441.java)|Easy| -|1442|[Count Triplets That Can Form Two Arrays of Equal XOR](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/)|c|[c++](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.cpp)|[python](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.py)|[go](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.go)|[js](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.js)|[java](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.java)|Medium| \ No newline at end of file +|1442|[Count Triplets That Can Form Two Arrays of Equal XOR](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/)|c|[c++](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.cpp)|[python](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.py)|[go](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.go)|[js](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.js)|[java](./src/1442-Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR/1442.java)|Medium| +|1443|[Minimum Time to Collect All Apples in a Tree](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/)|c|[c++](./src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp)|[python](./src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py)|[go](./src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go)|[js](./src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js)|[java](./src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java)|Medium| \ No newline at end of file diff --git a/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp new file mode 100644 index 00000000..dd19fa14 --- /dev/null +++ b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minTime(int n, vector>& edges, vector& 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> g; + + int dfs(int x, vector& 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; + } +}; \ No newline at end of file diff --git a/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go new file mode 100644 index 00000000..e2519373 --- /dev/null +++ b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go @@ -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 +} \ No newline at end of file diff --git a/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java new file mode 100644 index 00000000..77777275 --- /dev/null +++ b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java @@ -0,0 +1,30 @@ +class Solution { + public int minTime(int n, int[][] edges, List 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> g = new HashMap(); + + private int dfs(int x, List 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; + } +} \ No newline at end of file diff --git a/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js new file mode 100644 index 00000000..d23145cc --- /dev/null +++ b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js @@ -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); +}; \ No newline at end of file diff --git a/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py new file mode 100644 index 00000000..af5b3e01 --- /dev/null +++ b/src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py @@ -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) \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index 79deec0e..2dd7322c 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -2,9 +2,9 @@ import os, bisect # 题目名称 -name = "Count Triplets That Can Form Two Arrays of Equal XOR" -ID = 1442 -url = "https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/" +name = "Minimum Time to Collect All Apples in a Tree" +ID = 1443 +url = "https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/" difficult = "Medium" prog = ['c', 'cpp', 'py', 'go', 'js', 'java']