Skip to content

Commit

Permalink
add 1443
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed May 31, 2020
1 parent e016230 commit 4e68c24
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
|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|
30 changes: 30 additions & 0 deletions src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.cpp
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 src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.go
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 src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.java
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 src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.js
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 src/1443-Minimum-Time-to-Collect-All-Apples-in-a-Tree/1443.py
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)
6 changes: 3 additions & 3 deletions src/addProb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down

0 comments on commit 4e68c24

Please sign in to comment.