forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
69 lines (63 loc) · 1.74 KB
/
Solution.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
class LockingTree {
private Map<Integer, Integer> nums;
private int[] parent;
private List<Integer>[] children;
public LockingTree(int[] parent) {
nums = new HashMap<>();
this.parent = parent;
int n = parent.length;
children = new List[n];
Arrays.setAll(children, k -> new ArrayList<>());
for (int i = 0; i < n; ++i) {
if (parent[i] != -1) {
children[parent[i]].add(i);
}
}
}
public boolean lock(int num, int user) {
if (nums.containsKey(num)) {
return false;
}
nums.put(num, user);
return true;
}
public boolean unlock(int num, int user) {
if (!nums.containsKey(num) || nums.get(num) != user) {
return false;
}
nums.remove(num);
return true;
}
public boolean upgrade(int num, int user) {
int t = num;
while (t != -1) {
if (nums.containsKey(t)) {
return false;
}
t = parent[t];
}
boolean[] find = new boolean[1];
dfs(num, find);
if (!find[0]) {
return false;
}
nums.put(num, user);
return true;
}
private void dfs(int num, boolean[] find) {
for (int child : children[num]) {
if (nums.containsKey(child)) {
nums.remove(child);
find[0] = true;
}
dfs(child, find);
}
}
}
/**
* Your LockingTree object will be instantiated and called as such:
* LockingTree obj = new LockingTree(parent);
* boolean param_1 = obj.lock(num,user);
* boolean param_2 = obj.unlock(num,user);
* boolean param_3 = obj.upgrade(num,user);
*/