The problem is about finding the minimum value needed to make all nodes zero, let's call it K.
A non-binary tree of numbers is given.
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.
Then you should choose another node which has at least one zero-valued node at distance one, and repeat the process
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
Find the max node and start from it (if there is more than one max node, the one which comes sooner is chosen)
I define an array, let's call it possible nodes , and I add the node found in step 1 to it.
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.
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:
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:
max
and count how many nodes have that value => maxCount
maxCount = 1
then count how many nodes have the value max - 1
=> max1Count
, there are two possibilities:
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
max + 1
max
value which are highest up in the tree:
max
:
max
is equal to maxCount - 1
=> the solution is max + 1
max
and the count of its children with value max
is equal to maxCount - 2
=> the solution is max + 1
too max
but there are maxCount - 1
grand children which all have the same parent and value max
=> the solution is max + 1
also max + 2
maxCount
nodes and they all have the same parent => the solution is max + 1
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.