Advertisement
tinyevil

Untitled

Jun 30th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import functools
  2.  
  3. # floors - number of unchecked floors
  4. # eggs - number of remaining eggs
  5.  
  6. # returns (moves, floor)
  7. # where
  8. #   moves - is the number of moves required to determine the floor in the worst scenario
  9. #   floor - is the floor (1-based) from where you should drop the next egg
  10.  
  11. @functools.lru_cache(maxsize=None)
  12. def solve(floors, eggs):
  13.     if floors == 0:
  14.         return 0, 0
  15.     elif eggs == 1:
  16.         return floors, 1
  17.     else:
  18.         acc = 999999
  19.         drop = -1
  20.         for i in range(floors):
  21.             x, _ = solve(i, eggs - 1)
  22.             y, _ = solve(floors - i - 1, eggs)
  23.             a = max(x, y) + 1
  24.             if a < acc:
  25.                 acc = a
  26.                 drop = i + 1
  27.         return acc, drop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement