forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockingTree.java
110 lines (100 loc) · 2.81 KB
/
LockingTree.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
package g1901_2000.s1993_operations_on_tree;
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Tree #Design
// #2024_03_29_Time_58_ms_(99.38%)_Space_47.6_MB_(83.13%)
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unchecked")
public class LockingTree {
private List<Integer>[] graph;
private boolean[] locked;
private int[] parent;
private int[] users;
private int[] control;
public LockingTree(int[] parent) {
int n = parent.length;
this.parent = parent;
graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 1; i < n; i++) {
graph[parent[i]].add(i);
}
locked = new boolean[n];
users = new int[n];
control = new int[n];
}
private void setLock(int id, int user) {
locked[id] = true;
users[id] = user;
}
private void subNodeUnlock(int id) {
for (int child : graph[id]) {
locked[child] = false;
if (control[child] <= 0) {
continue;
}
control[child] = 0;
subNodeUnlock(child);
}
}
public boolean lock(int id, int user) {
if (locked[id]) {
return false;
}
setLock(id, user);
if (control[id] == 0) {
int node = parent[id];
while (node != -1) {
control[node]++;
if (locked[node] || control[node] > 1) {
break;
}
node = parent[node];
}
}
return true;
}
public boolean unlock(int id, int user) {
if (!locked[id] || users[id] != user) {
return false;
}
locked[id] = false;
if (control[id] == 0) {
int node = parent[id];
while (node != -1) {
control[node]--;
if (locked[node] || control[node] >= 1) {
break;
}
node = parent[node];
}
}
return true;
}
public boolean upgrade(int id, int user) {
if (locked[id] || control[id] == 0) {
return false;
}
int cur = parent[id];
while (cur != -1) {
if (locked[cur]) {
return false;
}
cur = parent[cur];
}
setLock(id, user);
if (control[id] > 0) {
control[id] = 0;
subNodeUnlock(id);
}
return true;
}
}
/*
* 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);
*/