Drop Eggs
If it is two eggs:
http://datagenetics.com/blog/july22012/index.html
public class Solution { /** * @param n an integer * @return an integer */ public int dropEggs(int n) { // Write your code here long ans = 0; for (int i = 1; ; ++i) { ans += (long)i; if (ans >= (long)n) return i; } } }
Imagine we drop our first egg from floor n, if it breaks, we can step through the previous (n-1) floors one-by-one.
If it doesn’t break, rather than jumping up another n floors, instead we should step up just (n-1) floors (because we have one less drop available if we have to switch to one-by-one floors), so the next floor we should try is floor n + (n-1)
Similarly, if this drop does not break, we next need to jump up to floor n + (n-1) + (n-2), then floor n + (n-1) + (n-2) + (n-3) …
We keep reducing the step by one each time we jump up, until that step-up is just one floor, and get the following equation for a 100 floor building:
n + (n-1) + (n-2) + (n-3) + (n-4) + … + 1 >= 100
This summation, as many will recognize, is the formula for triangular numbers (which kind of makes sense, since we’re reducing the step by one each drop we make) and can be simplified to:
n (n+1) / 2 >= 100
This is a quadratic equation, with the positive root of 13.651 (Which we have to round up to 14. This is not a John Malkovich movie!).
If there is as many as eggs:
Using binary search is the best.
What if you have n eggs:
http://www.geeksforgeeks.org/dynamic-programming-set-11-egg-dropping-puzzle/
Let's define our building to have a maximum of k floors. Let's say that the number of egg we have is represented by e, and the floor we are currently attempting to drop from is represented by n. With me so far? OK, what we need to find our optimal solution is to calculate what would happen if we dropped an egg from every floor (1 through to k) and (recursively) calculate the minimum number of droppings needed in the worst case. We're looking for the floor that gives the minimum solution to the worst case.
If we drop an egg from floor n, one of two events happens:
1) The egg breaks, so now our problem is reduced to a tower of height n-1, and we now have e-1 eggs.
2) The egg doesn't break and now we need to check k-n floors and we still have e eggs.
Remember we need to mimimize the number of drops in the worst case, so we take the higher (max) of these two situations, and select the floor which yields the minimum number of drops.
/* Function to get minimum number of trials needed in worst case with n eggs and k floors */ int eggDrop(int n, int k) { // If there are no floors, then no trials needed. OR if there is // one floor, one trial needed. if (k == 1 || k == 0) return k; // We need k trials for one egg and k floors if (n == 1) return k; int min = INT_MAX, x, res; // Consider all droppings from 1st floor to kth floor and // return the minimum of these values plus 1. for (x = 1; x <= k; x++) { res = max(eggDrop(n-1, x-1), eggDrop(n, k-x)); if (res < min) min = res; } return min + 1; }
Optimize:
class EggDrop { // A utility function to get maximum of two integers static int max(int a, int b) { return (a > b)? a: b; } /* Function to get minimum number of trials needed in worst case with n eggs and k floors */ static int eggDrop(int n, int k) { /* A 2D table where entery eggFloor[i][j] will represent minimum number of trials needed for i eggs and j floors. */ int eggFloor[][] = new int[n+1][k+1]; int res; int i, j, x; // We need one trial for one floor and0 trials for 0 floors for (i = 1; i <= n; i++) { eggFloor[i][1] = 1; eggFloor[i][0] = 0; } // We always need j trials for one egg and j floors. for (j = 1; j <= k; j++) eggFloor[1][j] = j; // Fill rest of the entries in table using optimal substructure // property for (i = 2; i <= n; i++) { for (j = 2; j <= k; j++) { eggFloor[i][j] = Integer.MAX_VALUE; for (x = 1; x <= j; x++) { res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]); if (res < eggFloor[i][j]) eggFloor[i][j] = res; } } } // eggFloor[n][k] holds the result return eggFloor[n][k]; }
posted on 2017-05-02 09:02 codingEskimo 阅读(334) 评论(0) 编辑 收藏 举报