Skip to content

Commit 2df7764

Browse files
authored
Added task 1993.
1 parent c5fc936 commit 2df7764

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g1901_2000.s1993_operations_on_tree;
2+
3+
// #Medium #Hash_Table #Breadth_First_Search #Tree #Design
4+
// #2022_05_19_Time_394_ms_(23.03%)_Space_167.4_MB_(5.26%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
public class LockingTree {
12+
private int[][] a;
13+
private HashMap<Integer, List<Integer>> map = new HashMap<>();
14+
15+
public LockingTree(int[] parent) {
16+
int l = parent.length;
17+
a = new int[l][2];
18+
for (int i = 0; i < l; i++) {
19+
a[i][0] = parent[i];
20+
a[i][1] = -1;
21+
map.putIfAbsent(parent[i], new ArrayList<>());
22+
List<Integer> p = map.get(parent[i]);
23+
p.add(i);
24+
map.put(parent[i], p);
25+
}
26+
}
27+
28+
public boolean lock(int num, int user) {
29+
int userId = a[num][1];
30+
if (userId == -1) {
31+
a[num][1] = user;
32+
return true;
33+
}
34+
return false;
35+
}
36+
37+
public boolean unlock(int num, int user) {
38+
int y = a[num][1];
39+
if (y == user) {
40+
a[num][1] = -1;
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
public boolean upgrade(int num, int user) {
47+
int par = num;
48+
while (par >= 0) {
49+
int lop = a[par][1];
50+
if (lop != -1) {
51+
return false;
52+
}
53+
par = a[par][0];
54+
}
55+
int f = 0;
56+
LinkedList<Integer> que = new LinkedList<>();
57+
int[] v = new int[a.length];
58+
que.add(num);
59+
v[num] = 1;
60+
while (!que.isEmpty()) {
61+
int t = que.get(0);
62+
que.remove(0);
63+
List<Integer> p = map.getOrDefault(t, new ArrayList<>());
64+
for (int e : p) {
65+
if (a[e][1] != -1) {
66+
f = 1;
67+
a[e][1] = -1;
68+
}
69+
if (v[e] == 0) {
70+
que.add(e);
71+
v[e] = 1;
72+
}
73+
}
74+
}
75+
if (f == 1) {
76+
a[num][1] = user;
77+
return true;
78+
}
79+
return false;
80+
}
81+
}
82+
83+
/*
84+
* Your LockingTree object will be instantiated and called as such:
85+
* LockingTree obj = new LockingTree(parent);
86+
* boolean param_1 = obj.lock(num,user);
87+
* boolean param_2 = obj.unlock(num,user);
88+
* boolean param_3 = obj.upgrade(num,user);
89+
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
1993\. Operations on Tree
2+
3+
Medium
4+
5+
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.
6+
7+
The data structure should support the following functions:
8+
9+
* **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.
10+
* **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.
11+
* **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:
12+
* The node is unlocked,
13+
* It has at least one locked descendant (by **any** user), and
14+
* It does not have any locked ancestors.
15+
16+
Implement the `LockingTree` class:
17+
18+
* `LockingTree(int[] parent)` initializes the data structure with the parent array.
19+
* `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`.
20+
* `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**.
21+
* `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**.
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/07/29/untitled.png)
26+
27+
**Input**
28+
29+
["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"]
30+
31+
[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]]
32+
33+
**Output:** [null, true, false, true, true, true, false]
34+
35+
**Explanation:**
36+
37+
LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]);
38+
lockingTree.lock(2, 2); // return true because node 2 is unlocked.
39+
// Node 2 will now be locked by user 2.
40+
lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2.
41+
lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2.
42+
// Node 2 will now be unlocked.
43+
lockingTree.lock(4, 5); // return true because node 4 is unlocked.
44+
// Node 4 will now be locked by user 5.
45+
lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4).
46+
// Node 0 will now be locked by user 1 and node 4 will now be unlocked.
47+
lockingTree.lock(0, 1); // return false because node 0 is already locked.
48+
49+
**Constraints:**
50+
51+
* `n == parent.length`
52+
* `2 <= n <= 2000`
53+
* `0 <= parent[i] <= n - 1` for `i != 0`
54+
* `parent[0] == -1`
55+
* `0 <= num <= n - 1`
56+
* <code>1 <= user <= 10<sup>4</sup></code>
57+
* `parent` represents a valid tree.
58+
* At most `2000` calls **in total** will be made to `lock`, `unlock`, and `upgrade`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1901_2000.s1993_operations_on_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class LockingTreeTest {
9+
@Test
10+
void lockingTree() {
11+
LockingTree lockingTree = new LockingTree(new int[] {-1, 0, 0, 1, 1, 2, 2});
12+
// return true because node 2 is unlocked.
13+
assertThat(lockingTree.lock(2, 2), equalTo(true));
14+
// Node 2 will now be locked by user 2.
15+
// return false because user 3 cannot unlock a node locked by user 2.
16+
assertThat(lockingTree.unlock(2, 3), equalTo(false));
17+
// return true because node 2 was previously locked by user 2.
18+
assertThat(lockingTree.unlock(2, 2), equalTo(true));
19+
// Node 2 will now be unlocked.
20+
// return true because node 4 is unlocked.
21+
assertThat(lockingTree.lock(4, 5), equalTo(true));
22+
// Node 4 will now be locked by user 5.
23+
lockingTree.upgrade(0, 1);
24+
// Node 0 will now be locked by user 1 and node 4 will now be unlocked.
25+
// return false because node 0 is already locked.
26+
assertThat(lockingTree.lock(0, 1), equalTo(false));
27+
}
28+
}

0 commit comments

Comments
 (0)