-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1993.java
121 lines (109 loc) · 4.26 KB
/
_1993.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
111
112
113
114
115
116
117
118
119
120
121
package com.fishercoder.solutions.secondthousand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _1993 {
public static class Solution1 {
/*
* My completely original solution:
* 1. use hashmap1 to store num to node mapping;
* 2. use hashmap2 to store num to user lock mapping;
* 3. use hashmap3 to store child to parent mapping;
* 4. build the tree: make sure to retrieve the node from map if it exists, otherwise, the tree might be disconnected, i.e. leading to wrong ansers.
*/
public static class LockingTree {
class TreeNode {
List<TreeNode> children;
int val;
public TreeNode(int val) {
this.val = val;
this.children = new ArrayList<>();
}
}
Map<Integer, TreeNode> map;
Map<TreeNode, TreeNode> childToParentMap;
Map<Integer, Integer> lockMap;
TreeNode root;
public LockingTree(int[] parent) {
this.map = new HashMap<>();
this.root = new TreeNode(0);
this.map.put(0, root);
this.childToParentMap = new HashMap<>();
constructTree(parent, map, childToParentMap);
this.lockMap = new HashMap<>();
}
private void constructTree(
int[] parent,
Map<Integer, TreeNode> map,
Map<TreeNode, TreeNode> childToParentMap) {
for (int i = 1; i < parent.length; i++) {
TreeNode parentNode = map.getOrDefault(parent[i], new TreeNode(parent[i]));
TreeNode childNode = map.getOrDefault(i, new TreeNode(i));
parentNode.children.add(childNode);
map.put(parent[i], parentNode);
map.put(i, childNode);
childToParentMap.put(childNode, parentNode);
}
}
public boolean lock(int num, int user) {
if (lockMap.containsKey(num)) {
return false;
} else {
lockMap.put(num, user);
return true;
}
}
public boolean unlock(int num, int user) {
if (!lockMap.containsKey(num)) {
return false;
} else if (lockMap.get(num) == user || user == -1) {
lockMap.remove(num);
return true;
} else {
return false;
}
}
public boolean upgrade(int num, int user) {
if (hasLockedAncestor(num) || !hasOneLockedChild(num) || lockMap.containsKey(num)) {
return false;
}
lock(num, user);
List<TreeNode> children = map.get(num).children;
for (TreeNode child : children) {
unlockRegardlessUser(child);
}
return true;
}
private boolean hasOneLockedChild(int num) {
if (lockMap.containsKey(num)) {
return true;
}
TreeNode node = map.get(num);
for (TreeNode child : node.children) {
if (hasOneLockedChild(child.val)) {
return true;
}
}
return false;
}
private boolean hasLockedAncestor(int num) {
TreeNode node = map.get(num);
while (childToParentMap.containsKey(node)) {
TreeNode parent = childToParentMap.get(node);
if (lockMap.containsKey(parent.val)) {
return true;
}
node = parent;
}
return false;
}
private void unlockRegardlessUser(TreeNode treeNode) {
unlock(treeNode.val, -1);
for (TreeNode child : treeNode.children) {
unlockRegardlessUser(child);
}
}
}
}
}