3

It is related to this questioN : Two marbles and a 100 story building BUT it is not the same ... We are to figure out best algorithm to figure out, strategy to minimize maximum drops required to find lowest floor ..

Here is what I have on top of my mind

The last marble has to be dropped in stepwise fashion

Rest of the marbles will choose a hop (say hop-n)

e.g.. So when N = 2, M = 100, We know that maximum drops=14 and hop-1 = the floor from which first marble will be dropped for very first time.

Community
  • 1
  • 1
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
  • This is mathematical, not development task, and posting answer here will probably make it less interesting for others. – Mikhail Vladimirov Feb 17 '13 at 09:18
  • You probaly want `std::find_lowest_marble_break_floor`. Oh hang on, C++ knows nothing about marble problems. – juanchopanza Feb 17 '13 at 09:18
  • @MikhailVladimirov : I did not get you. You think the algorithm to derive this would be purely mathematical work ? – Ajeet Ganga Feb 17 '13 at 09:23
  • @Ajeet The point being made is that this is a mathematical/logical/algorithmic problem and SO concerns itself primarily with implementation issues or code related issues, thus it is rather off topic. – Karthik T Feb 17 '13 at 09:25

2 Answers2

6

Here is simple brute-force solution written in Java:

/**
 * Return maximum number of floors that could be checked with given number
 * of marbles and drops.
 */
private static int getMaximumFloors (int marblesCount, int dropsCount)
{
    if (marblesCount == 0) return 0;
    else
    {
        int result = 0;

        for (int i = 0; i < dropsCount; i++)
        {
            result += getMaximumFloors (marblesCount - 1, i) + 1;
        }

        return result;
    }
}

/**
 * Return minimum number of drops that is definitely enough to check
 * given number of floors having given number of marbles.
 */
private static int getMinimumDrops (int marblesCount, int floorsCount)
{
    int dropsCount = 0;
    while (getMaximumFloors (marblesCount, dropsCount) < floorsCount)
        dropsCount += 1;
    return dropsCount;
}

public static void main (String [] args)
{
    System.out.println (getMinimumDrops (2, 100));
}

It should be easy to port it to C/C++.

Here are some results:

2 marbles, 100 floors -> 14
3 marbles, 100 floors -> 9
4 marbles, 100 floors -> 8
5 marbles, 100 floors -> 7
6 marbles, 100 floors -> 7
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • I'm not a java guy; can you share out put for (2, 100) & (3, 100). Also Complexity. – जलजनक Feb 17 '13 at 09:32
  • 2
    Sure, added to answer. Complexity is surely bad, because this is brute force. I think it is nice mathematical task to reduce this algorithm to direct formula. At least for one and two marbles it is easy. – Mikhail Vladimirov Feb 17 '13 at 09:34
  • As an explanation to what the code is doing: the maximum number of floors can be explored with n marbles and m drops is `Sum[i1 = 1..m] (Sum [i2 = 1..i1] (... Sum[in = 1..i(n-1)](in))...)` (n layers of sums). The strategy is for the 1st marble, we will start dropping from the floor calculated by setting i1 = m, then i1 = m-1..m, etc. in the equation until it breaks. The subsequent marble are also drop from more floor to less floor, calculated by the equation above but striped off of the outer layers. – nhahtdh Feb 17 '13 at 10:18
4

Number of floors F that can be explored with M marbles and D drops is

F(0,D) = 0 /* no marbles, no results */
F(M,0) = 0 /* no drops, no results */

F(M,D) = 1 + /* we drop a marble once... */
    F(M-1,D-1) + /* it breaks ... */
    F(M,D-1) /* or it doesn't */

This function is a reverse of what you want to calculate, but this is not a problem. The function is monotonic so just do a binary search in the result space.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243