Very interesting Tree problem II

Another interesting Tree problem, this time more of a design problem: design and implement a locking tree structure. I used hashtable to represent the tree, another hashtable to represent the locked elements, and a copy of the parents array. The upgrade method is O(N), and since it may get called N times, the total complexity is N^2 where N=2x10^3 (reasonable). Full implementation down below, cheers, ACC.

Operations on Tree - LeetCode

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 ith 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.

The data structure should support the following functions:

  • 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.
  • 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.
  • 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:
    • The node is unlocked,
    • It has at least one locked descendant (by any user), and
    • It does not have any locked ancestors.

Implement the LockingTree class:

  • LockingTree(int[] parent) initializes the data structure with the parent array.
  • 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.
  • 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.
  • 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.

Example 1:

Input
["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"]
[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]]
Output
[null, true, false, true, true, true, false]
Explanation
LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]);
lockingTree.lock(2, 2); // return true because node 2 is unlocked.
 // Node 2 will now be locked by user 2.
lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2.
lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2.
 // Node 2 will now be unlocked.
lockingTree.lock(4, 5); // return true because node 4 is unlocked.
 // Node 4 will now be locked by user 5.
lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4).
 // Node 0 will now be locked by user 1 and node 4 will now be unlocked.
lockingTree.lock(0, 1); // return false because node 0 is already locked.

Constraints:

  • n == parent.length
  • 2 <= n <= 2000
  • 0 <= parent[i] <= n - 1 for i != 0
  • parent[0] == -1
  • 0 <= num <= n - 1
  • 1 <= user <= 104
  • parent represents a valid tree.
  • At most 2000 calls in total will be made to lock, unlock, and upgrade.
Accepted
14,097
Submissions
31,139

public class LockingTree
{
 private Hashtable tree = null;
 private int[] parent = null;
 private Hashtable nodeLock = null;
 public LockingTree(int[] parent)
 {
 tree = new Hashtable();
 this.parent = new int[parent.Length];
 nodeLock = new Hashtable();
 for (int i = 0; i < parent.Length; i++)
 {
 this.parent[i] = parent[i];
 if (!tree.ContainsKey(parent[i])) tree.Add(parent[i], new Hashtable());
 Hashtable children = (Hashtable)tree[parent[i]];
 if (!children.ContainsKey(i)) children.Add(i, true);
 }
 }
 public bool Lock(int num, int user)
 {
 if (!nodeLock.ContainsKey(num))
 {
 nodeLock.Add(num, user);
 return true;
 }
 return false;
 }
 public bool Unlock(int num, int user)
 {
 if (!nodeLock.ContainsKey(num) || (int)nodeLock[num] != user) return false;
 nodeLock.Remove(num);
 return true;
 }
 public bool Upgrade(int num, int user)
 {
 if (!nodeLock.ContainsKey(num) && HasAnyLockedDescendant(num) && !HasAnyLockedAnscestor(num))
 {
 nodeLock.Add(num, user);
 UnlockAllDescendants(num);
 return true;
 }
 return false;
 }
 private void UnlockAllDescendants(int node)
 {
 if (tree.ContainsKey(node))
 {
 Hashtable children = (Hashtable)tree[node];
 if (children != null)
 {
 foreach (int child in children.Keys)
 _UnlockAllDescendants(child);
 }
 }
 }
 private void _UnlockAllDescendants(int node)
 {
 if (nodeLock.ContainsKey(node)) nodeLock.Remove(node);
 if (tree.ContainsKey(node))
 {
 Hashtable children = (Hashtable)tree[node];
 if (children != null)
 {
 foreach (int child in children.Keys)
 _UnlockAllDescendants(child);
 }
 }
 }
 private bool HasAnyLockedDescendant(int node)
 {
 if (tree.ContainsKey(node))
 {
 Hashtable children = (Hashtable)tree[node];
 if (children != null)
 {
 foreach (int child in children.Keys)
 {
 if (nodeLock.ContainsKey(child) || HasAnyLockedDescendant(child)) return true;
 }
 }
 }
 return false;
 }
 private bool HasAnyLockedAnscestor(int node)
 {
 int parentNode = parent[node];
 while (parentNode != -1)
 {
 if (nodeLock.ContainsKey(parentNode)) return true;
 parentNode = parent[parentNode];
 }
 return false;
 }
}

Comments

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...