imashutosh51

Egg Dropping problem

Mar 20th, 2026 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. """
  2. Intuition for Egg Dropping Problem (Step-by-Step Breakdown)
  3.  
  4. Step 1: Identify the Problem Type
  5. ---------------------------------
  6. We are given 'e' eggs and 'f' floors. We need to find the MINIMUM number
  7. of attempts needed in the WORST CASE to find the threshold floor (the
  8. highest floor from which an egg doesn't break). Because we are looking
  9. for an optimal strategy amidst uncertainty, this falls under the
  10. Matrix Chain Multiplication (MCM) pattern of 'partitioning'.
  11.  
  12. Step 2: Define the Range (The 'k' Loop)
  13. ---------------------------------------
  14. In MCM, we split a range [i, j]. Here, our 'range' is the number of
  15. floors. We can choose to drop an egg from any floor 'k' where
  16. 1 <= k <= f. This 'k' acts as our partition point.
  17.  
  18. Step 3: The Base Cases (The 'Small' Problems)
  19. ---------------------------------------------
  20. - If floors (f) == 0 or 1: We need 0 or 1 attempts respectively.
  21. - If eggs (e) == 1: We must start from the 1st floor and go up
  22.  sequentially. In the worst case, the threshold is the top floor,
  23.  so we need 'f' attempts.
  24.  
  25. Step 4: The Recursive 'Worst Case' Logic
  26. -----------------------------------------
  27. When we drop an egg from floor 'k', only two things can happen:
  28.  
  29. Scenario 1: The Egg BREAKS
  30.   - We now have (e - 1) eggs.
  31.   - We only need to check floors BELOW 'k'.
  32.   - New problem: solve(e - 1, k - 1)
  33.  
  34. Scenario 2: The Egg DOES NOT BREAK
  35.   - We still have 'e' eggs.
  36.   - We only need to check floors ABOVE 'k' (total f - k floors).
  37.   - New problem: solve(e, f - k)
  38.  
  39. Step 5: Finding the 'Worst Case' for a Split
  40. ---------------------------------------------
  41. Since we want to be certain of our answer regardless of whether the egg
  42. breaks or not, we must consider the WORST of these two scenarios:
  43. Worst Case at floor 'k' = 1 + max(solve(e-1, k-1), solve(e, f-k))
  44. (The '1' represents the current attempt we just made).
  45.  
  46. Step 6: Minimizing the Attempts
  47. -------------------------------
  48. We repeat this calculation for every possible floor 'k' from 1 to f.
  49. Our final answer is the MINIMUM of all these worst-case values, as we
  50. are looking for the most efficient strategy.
  51. Ans = min(for all k: 1 + max(break, no_break))
  52.  
  53. Step 7: Optimization (Memoization)
  54. ----------------------------------
  55. The recursive tree has many overlapping sub-problems (e.g., calculating
  56. 3 eggs and 5 floors multiple times). We use a 2D array or a map
  57. table[e+1][f+1] to store results and avoid TLE (Time Limit Exceeded).
  58.  
  59. T.C: We are checking on all e eggs and all f floors so e*f states and for each states , work needs to be done is O(f) ie. loop so Total= O(e*f*f)=O(ef^2)
  60. We are storing the e*f states so S.C=O(e*f)
  61. """
  62.  
  63.  
  64. from math import inf
  65. def fun(e,f,dp):
  66.     if e==1:
  67.         return f
  68.     if f<=1:
  69.         return f
  70.     if dp[f][e]!=-1:
  71.         return dp[f][e]
  72.     _min=inf
  73.     for k in range(1,f+1):
  74.         temp=1+ max(fun(e-1,k-1,dp), fun(e,f-k,dp))
  75.         _min=min(temp,_min)
  76.     dp[f][e]=_min
  77.     return dp[f][e]
  78.    
  79.  
  80. class Solution:
  81.     def eggDrop(self, e, f):
  82.         dp=[[-1]*(e+1) for _ in range(f+1)]
  83.         return fun(e,f,dp)
  84.  
  85.  
  86. #M-2 More optimised approach T.C O(e*f*log(f))
  87. from math import inf
  88.  
  89. def fun(e, f, dp):
  90.  
  91.     if e == 1:
  92.         return f
  93.  
  94.     if f <= 1:
  95.         return f
  96.  
  97.     if dp[e][f] != -1:
  98.         return dp[e][f]
  99.  
  100.     low = 1
  101.     high = f
  102.  
  103.     ans = inf
  104.  
  105.     while low <= high:
  106.  
  107.         mid = (low + high) // 2
  108.  
  109.         break_case = fun(e - 1, mid - 1, dp)
  110.         survive_case = fun(e, f - mid, dp)
  111.  
  112.         temp = 1 + max(break_case, survive_case)
  113.  
  114.         ans = min(ans, temp)
  115.  
  116.         # move towards larger side
  117.         if break_case < survive_case:
  118.             low = mid + 1
  119.         else:
  120.             high = mid - 1
  121.  
  122.     dp[e][f] = ans
  123.  
  124.     return ans
  125.  
  126.  
  127. class Solution:
  128.     def eggDrop(self, e, f):
  129.  
  130.         dp = [[-1] * (f + 1) for _ in range(e + 1)]
  131.  
  132.         return fun(e, f, dp)
Advertisement
Add Comment
Please, Sign In to add comment