简体   繁体   中英

Find minimum value needed to make all tree nodes zero by incrementing asjacent nodes at each step

  1. The problem is about finding the minimum value needed to make all nodes zero, let's call it K.

  2. A non-binary tree of numbers is given.

  3. In the first step you can choose one of the nodes to start at. If K is greater than that node's value, you change that value to zero, and increment the value of the other nodes which are at a distance of one or two. Note that once a node's value becomes zero, it wont be incremented any more and it wont allow nodes which are connected to it be incremented.

  4. Then you should choose another node which has at least one zero-valued node at distance one, and repeat the process

Example:

5
 \
  2 
   \
    5

when we start from the leaf with value 5, we have

6
 \
  3
   \
    0

Then we should choose 3; we can't choose 6 because it has no zero node in distance one!

7
 \
  0
   \
    0

At the end we choose 7 and K = 7 but if we choose 2 at first then we have:

6
 \
  0
   \
    6

Then we should choose 6; because the node with value two is zero now, the connection is cut and by changing value of node with value six, no more increment would happen!

0
 \
  0
   \
    6

So minimum K = 6

My current approach:

  1. Find the max node and start from it (if there is more than one max node, the one which comes sooner is chosen)

  2. I define an array, let's call it possible nodes , and I add the node found in step 1 to it.

  3. While possible nodes is not empty, I perform the following steps:

    a. Choose the max value in possible nodes; let's call it max_node

    b. Make max_node power zero and update K

    c. Increment the value of its parent, grand parent, children, grand children and siblings (if they were not zero before)

    d. Add its parent and children with non-zero values to possible nodes

    e. Remove max_node from possible nodes

Actually this is a homework problem, but this approach is not the right one! It gives wrong answers and hits time-out limits.

Constraints

  • Number of nodes ≤ 3×10 5

  • -10 9 ≤ value of nodes ≤ 10 9

  • Time limit: 2.5 seconds

  • Memory limit: 256 MB

If you start with a random node in the tree, then K is the maximum of:

  1. the start node
  2. its children + 1
  3. its parent + 1
  4. all other nodes + 2

This is because of the restriction that you can only choose nodes with 0-nodes adjacent to it and that those are not incremented.

We can conclude that Kmin has to be between max and max + 2.

So an O(n) algorithm could look like this:

  1. find the maximum node value max and count how many nodes have that value => maxCount
  2. if maxCount = 1 then count how many nodes have the value max - 1 => max1Count , there are two possibilities:
    1. max1Count = 0 or the number of adjacent nodes with value max - 1 to the node with value max is equal to max1Count => the solution is max
    2. otherwise the solution is max + 1
  3. find all the nodes with max value which are highest up in the tree:
    1. if you found only one node for the depth of the first occurrence of max :
      1. the number of children with value max is equal to maxCount - 1 => the solution is max + 1
      2. there is only one child with value max and the count of its children with value max is equal to maxCount - 2 => the solution is max + 1 too
      3. there is no child with value max but there are maxCount - 1 grand children which all have the same parent and value max => the solution is max + 1 also
      4. otherwise the solution is max + 2
    2. if you found maxCount nodes and they all have the same parent => the solution is max + 1
    3. otherwise the solution is max + 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM