|
| 1 | +# 1993. Operations on Tree |
| 2 | +You are given a tree with `n` nodes numbered from `0` to `n - 1` in the form of a parent array `parent` where `parent[i]` is the parent of the <code>i<sup>th</sup></code> node. The root of the tree is node `0`, so `parent[0] = -1` since it has no parent. You want to design a data structure that allows users to lock, unlock, and upgrade nodes in the tree. |
| 3 | + |
| 4 | +The data structure should support the following functions: |
| 5 | + |
| 6 | +* **Lock: Locks** the given node for the given user and prevents other users from locking the same node. You may only lock a node using this function if the node is unlocked. |
| 7 | +* **Unlock: Unlocks** the given node for the given user. You may only unlock a node using this function if it is currently locked by the same user. |
| 8 | +* **Upgrade: Locks** the given node for the given user and **unlocks** all of its descendants **regardless** of who locked it. You may only upgrade a node if **all** 3 conditions are true: |
| 9 | + * The node is unlocked, |
| 10 | + * It has at least one locked descendant (by **any** user), and |
| 11 | + * It does not have any locked ancestors. |
| 12 | + |
| 13 | +Implement the `LockingTree` class: |
| 14 | + |
| 15 | +* `LockingTree(int[] parent)` initializes the data structure with the parent array. |
| 16 | +* `lock(int num, int user)` returns `true` if it is possible for the user with id `user` to lock the node `num`, or `false` otherwise. If it is possible, the node `num` will become **locked** by the user with id `user`. |
| 17 | +* `unlock(int num, int user)` returns `true` if it is possible for the user with id `user` to unlock the node `num`, or `false` otherwise. If it is possible, the node `num` will become **unlocked**. |
| 18 | +* `upgrade(int num, int user)` returns `true` if it is possible for the user with id `user` to upgrade the node `num`, or `false` otherwise. If it is possible, the node `num` will be **upgraded**. |
| 19 | + |
| 20 | +#### Example 1: |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> |
| 24 | +["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"] |
| 25 | +[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]] |
| 26 | +<strong>Output:</strong> |
| 27 | +[null, true, false, true, true, true, false] |
| 28 | +<strong>Explanation:</strong> |
| 29 | +LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]); |
| 30 | +lockingTree.lock(2, 2); // return true because node 2 is unlocked. |
| 31 | + // Node 2 will now be locked by user 2. |
| 32 | +lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2. |
| 33 | +lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2. |
| 34 | + // Node 2 will now be unlocked. |
| 35 | +lockingTree.lock(4, 5); // return true because node 4 is unlocked. |
| 36 | + // Node 4 will now be locked by user 5. |
| 37 | +lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4). |
| 38 | + // Node 0 will now be locked by user 1 and node 4 will now be unlocked. |
| 39 | +lockingTree.lock(0, 1); // return false because node 0 is already locked. |
| 40 | +</pre> |
| 41 | + |
| 42 | +#### Constraints: |
| 43 | +* `n == parent.length` |
| 44 | +* `2 <= n <= 2000` |
| 45 | +* `0 <= parent[i] <= n - 1` for `i != 0` |
| 46 | +* `parent[0] == -1` |
| 47 | +* `0 <= num <= n - 1` |
| 48 | +* <code>1 <= user <= 10<sup>4</sup></code> |
| 49 | +* `parent` represents a valid tree. |
| 50 | +* At most `2000` calls **in total** will be made to `lock`, `unlock`, and `upgrade`. |
| 51 | + |
| 52 | +## Solutions (Rust) |
| 53 | + |
| 54 | +### 1. Solution |
| 55 | +```Rust |
| 56 | +struct LockingTree { |
| 57 | + parent: Vec<i32>, |
| 58 | + children: Vec<Vec<i32>>, |
| 59 | + locked: Vec<i32>, |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * `&self` means the method takes an immutable reference. |
| 64 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 65 | + */ |
| 66 | +impl LockingTree { |
| 67 | + fn new(parent: Vec<i32>) -> Self { |
| 68 | + let n = parent.len(); |
| 69 | + let mut children = vec![vec![]; n]; |
| 70 | + |
| 71 | + for i in 1..n { |
| 72 | + children[parent[i] as usize].push(i as i32); |
| 73 | + } |
| 74 | + |
| 75 | + Self { |
| 76 | + parent: parent, |
| 77 | + children: children, |
| 78 | + locked: vec![-1; n], |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fn lock(&mut self, num: i32, user: i32) -> bool { |
| 83 | + if self.locked[num as usize] == -1 { |
| 84 | + self.locked[num as usize] = user; |
| 85 | + |
| 86 | + true |
| 87 | + } else { |
| 88 | + false |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn unlock(&mut self, num: i32, user: i32) -> bool { |
| 93 | + if self.locked[num as usize] == user { |
| 94 | + self.locked[num as usize] = -1; |
| 95 | + |
| 96 | + true |
| 97 | + } else { |
| 98 | + false |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fn upgrade(&mut self, num: i32, user: i32) -> bool { |
| 103 | + let mut curr = num; |
| 104 | + let mut nodes = self.children[num as usize].clone(); |
| 105 | + let mut locked_descendants = vec![]; |
| 106 | + |
| 107 | + while curr != -1 { |
| 108 | + if self.locked[curr as usize] != -1 { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + curr = self.parent[curr as usize]; |
| 113 | + } |
| 114 | + |
| 115 | + while let Some(node) = nodes.pop() { |
| 116 | + nodes.append(&mut self.children[node as usize].clone()); |
| 117 | + |
| 118 | + if self.locked[node as usize] != -1 { |
| 119 | + locked_descendants.push(node as usize); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if !locked_descendants.is_empty() { |
| 124 | + self.lock(num, user); |
| 125 | + for node in locked_descendants { |
| 126 | + self.locked[node] = -1; |
| 127 | + } |
| 128 | + |
| 129 | + true |
| 130 | + } else { |
| 131 | + false |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Your LockingTree object will be instantiated and called as such: |
| 138 | + * let obj = LockingTree::new(parent); |
| 139 | + * let ret_1: bool = obj.lock(num, user); |
| 140 | + * let ret_2: bool = obj.unlock(num, user); |
| 141 | + * let ret_3: bool = obj.upgrade(num, user); |
| 142 | + */ |
| 143 | +``` |
0 commit comments