-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSmallestMissingGeneticValueInEachSubtree.java
110 lines (102 loc) · 3.05 KB
/
SmallestMissingGeneticValueInEachSubtree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*https://leetcode.com/problems/smallest-missing-genetic-value-in-each-subtree/*/
class Solution {
int[] result, parents, nums;
SortedSet<Integer> set;
Map<Integer, List<Integer>> graph;
boolean[] removed;
int max;
public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {
int n = parents.length;
result = new int[n];
removed = new boolean[(int)1e5+1];
this.parents = parents;
this.nums = nums;
set = new TreeSet<Integer>();
Arrays.fill(result,1);
max = 0;
for (int val : nums)
max = Math.max(max,val);
for (int val = 1; val <= max+1; ++val)
set.add(val);
if (!set.contains(1)) return result;
graph = new HashMap<Integer,List<Integer>>();
for (int i = 0; i < n; ++i)
graph.put(i,new ArrayList<Integer>());
for (int i = 1; i < n; ++i)
graph.get(parents[i]).add(i);
dfs(0);
return result;
}
private boolean dfs(int node)
{
boolean isOnePresent = false;
if (nums[node] == 1) isOnePresent = true;
else for (Integer child : graph.get(node))
isOnePresent |= dfs(child);
if (isOnePresent)
{
remove(node);
result[node] = set.first();
}
return isOnePresent;
}
private void remove(int node)
{
int num = nums[node];
if (removed[num]) return;
removed[num] = true;
set.remove(num);
for (Integer child : graph.get(node))
remove(child);
}
}
class Solution {
int[] result, parents, nums;
SortedSet<Integer> set;
Map<Integer, List<Integer>> graph;
boolean[] removed;
int max;
public int[] smallestMissingValueSubtree(int[] parents, int[] nums) {
int n = parents.length;
result = new int[n];
removed = new boolean[(int)1e5+1];
this.parents = parents;
this.nums = nums;
set = new TreeSet<Integer>();
Arrays.fill(result,1);
max = 0;
int val, oneIndex = -1;
for (int i = 0; i < nums.length; ++i)
{
val = nums[i];
if (val == 1) oneIndex = i;
max = Math.max(max,val);
}
if (oneIndex == -1) return result;
for (val = 1; val <= max+1; ++val)
set.add(val);
graph = new HashMap<Integer,List<Integer>>();
for (int i = 0; i < n; ++i)
graph.put(i,new ArrayList<Integer>());
for (int i = 1; i < n; ++i)
graph.get(parents[i]).add(i);
dfs(oneIndex);
return result;
}
private void dfs(int node)
{
if (node == -1) return;
remove(node);
result[node] = set.first();
dfs(parents[node]);
}
private void remove(int node)
{
int num = nums[node];
if (removed[num]) return;
removed[num] = true;
set.remove(num);
for (Integer child : graph.get(node))
remove(child);
}
}