This problem can also be
modeled with Binary Search Tree (BST).
In order to facilitate our
analysis, let us draw BST with slight modifications as follows:
1. If
egg breaks then child node is drawn on left down side
2. If
egg does not break then child node is drawn straight down side
If we draw BST with above
kind of representation then width of the BST represents the number of eggs. Width
of BST is equal to number of vertical columns in BST.
Note that, for full BST
drawn with above kind of representation, width = height
Any BST with f number of
nodes, drawn with above kind of representation and subjected to the constraint width of
BST <= e (number of eggs) is a solution but it may not be the
optimal solution. Hence obtaining the optimal solution is equivalent to
obtaining the arrangement of nodes in BST with minimum height subjected to the
constraint: width of BST <= e
If full BST is possible with
f number of nodes and if it also satisfies above constraint then such a full
BST is itself the solution and height of the BST is optimum number of trials required.
Even if full BST is not
possible with given number of floors (f), but if following conditions are met
then also it is a solution:
e
>= h
where h is the height of the next possible full BST
Fh-1
< f <= Fh
Where Fh is the
total number of nodes in full BST having height = h
Hence for given number of
floors, first we will have to obtain the value of h. If e >=
h then solution for optimal number of trials required
= h.
But if e
< h then optimum number of
trials required will be definitely more than h.
In the BST drawn with our
special kind of representation, every column corresponds to a particular number
of egg breaks. For example, to reach any node in the right most column, we will
need zero egg breaks. Whereas to reach any node in the left most column, we
will need (width -1) number of egg breaks.
Hence, if e
< h then we need to discard all
the nodes in columns requiring more than e number of egg breaks. And these
nodes needs to be inserted below the leaf nodes in remaining valid columns.
But if f < Fh then
we need not insert all the nodes from invalid columns. The number of nodes to be
inserted below the leaf nodes of valid columns = (total number of nodes in
invalid columns) – (Fh-f) ---- (1)
If above
figure comes negative then our task is done, the optimal number of trials
required = h. We need not perform the removal process, because we are sure that
the optimal number of trials required are at least h because we did choose h
based on condition:
Fh-1
< f <= Fh
If the difference in (1) is
a positive number then we have to perform the process of addition/insertion of
nodes. In this case, we can keep e number of columns from right side and all
the nodes from (width-e) number of left columns needs to be inserted below the
e number of right columns. Hence obtaining optimal solution reduces to finding
a way to insert these extra nodes below the leaf nodes of e number of right
columns such that the max height of the BST is minimised.
Let us consider an example
as follows:
e =
2
f =
12
for above example h = 4
because 23 -1 < f <= 24-1
Full BST with our special
kind of representation of height = 4 is shown in the image above and its
details are as follows:
Column
number
(left to right)
|
Nodes
|
Number
of nodes
|
Leaf
nodes
|
Number
of leaf nodes
|
Number
of egg breaks required to reach these nodes
|
0
|
1
|
1
|
1
|
1
|
3
|
1
|
2 3
5 9
|
4
|
3 5 9
|
3
|
2
|
2
|
4 6
7 10 11
13
|
6
|
7 11
13
|
3
|
1
|
3
|
8 12
14 15
|
4
|
15
|
1
|
0
|
This full BST has width = 4
and hence is not a valid solution because we have only 2 eggs. We need to move
the nodes from columns 0 & 1 below the leaf nodes under columns 2 & 3.
The number of nodes at
different columns follows the pattern of binomial coefficients as follows:
Number of nodes at column c
= nc = h!/(c! (h-c)!)
Where c starts from 0 and
ends at (h-1)
Hence the total number of
nodes in first two columns to be removed from full BST = n0 + n1 = 4!/(0! (4 –
0)!) + 4!/(1! (4 – 1)!) = 1 + 4 = 5
But we need not insert all
the 5 nodes, because f < Fh
Number of nodes to be inserted
below the leaf nodes of columns 2 & 3 = 5 – (Fh-f) = 5 – (15 -
12) = 2
The number of leaf nodes at
different columns also follows the pattern of binomial coefficients as follows:
Number of leaf nodes at
column c = nlc =
(h-1)!/( c! (h-1-c)! )
Hence number of leaf nodes
at columns 2 & 3 = 3!/(2! (4–1-2)!) + 3!/ (3! (4–1-3)!)= 3 + 1 = 4
Since in this case the
number of leaf nodes is more than the number of nodes to be inserted, solution
is trivial and is equal to 4 + 1 = 5
But if number of leaf nodes
is less than the number of nodes to be inserted then insertion of nodes needs
to be done iteratively and the leaf nodes at each iteration goes on increasing
as follows :
Suppose the number of leaf nodes at
valid columns = [4 6 1] and the number of nodes to be inserted = 30 then
we have to perform two iterations as follows and the height will be 2 + height
of full BST
Iteration number
|
Number of nodes that can be inserted in this iteration
=
number of leaf nodes at left most valid column
+
2 * number of leaf nodes at remaining valid columns
|
Cumulative number of nodes inserted
|
New leaf nodes at valid columns
=
Current number of leaf nodes
+
current number of leaf nodes at right column
At right most column the number of leaf nodes stays same at
all the iterations.
|
1
|
= 4 + 2*6 + 2 * 1
= 18
|
18
|
(4+6) (6+1) 1
= 10 7 1
|
2
|
= 10 + 2*7 + 2*1
= 26
|
44
|
(10+7) (7+1) 1
= 17 8 1
|