<p>You are given <code>k</code> identical eggs and you have access to a building with <code>n</code> floors labeled from <code>1</code> to <code>n</code>.</p>

<p>You know that there exists a floor <code>f</code> where <code>0 &lt;= f &lt;= n</code> such that any egg dropped at a floor <strong>higher</strong> than <code>f</code> will <strong>break</strong>, and any egg dropped <strong>at or below</strong> floor <code>f</code> will <strong>not break</strong>.</p>

<p>Each move, you may take an unbroken egg and drop it from any floor <code>x</code> (where <code>1 &lt;= x &lt;= n</code>). If the egg breaks, you can no longer use it. However, if the egg does not break, you may <strong>reuse</strong> it in future moves.</p>

<p>Return <em>the <strong>minimum number of moves</strong> that you need to determine <strong>with certainty</strong> what the value of </em><code>f</code> is.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> k = 1, n = 2
<strong>Output:</strong> 2
<strong>Explanation: </strong>
Drop the egg from floor 1. If it breaks, we know that f = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
If it does not break, then we know f = 2.
Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> k = 2, n = 6
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> k = 3, n = 14
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= 100</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    @cache
    def superEggDrop(self, k: int, n: int) -> int:

        if n == 0:
            return 0
        
        # with 1 egg, need to try from floor 1 up till n. if egg breaks at floor n, need n tries.
        if k == 1:
            return n
        
        res = n

        # (knlogn)
        # notice as x increases and k stays same, dp[k][x] increases.
        # as x increases, left = dp[k-1][x-1] increases, right = dp[k][n-x] decreases.
        # use binary search to get the minimum max(left, right) + 1 when left and right are as close as possible.

        lo, hi = 1, n

        while lo <= hi:
            mid = (lo + hi) // 2
            left = self.superEggDrop(k - 1, mid - 1)
            right = self.superEggDrop(k, n - mid)
            res = min(res, max(left, right) + 1)
            if left == right:
                break
            elif left < right:
                lo = mid + 1
            else:
                hi = mid - 1

        # (kn^2)
        # # try drop at every floor x, 1 <= x <= n
        # for x in range(1, n + 1):
        #     # breaks at floor x
        #     a = self.superEggDrop(k - 1, x - 1)
        #     # doesnt break
        #     b = self.superEggDrop(k, n - x)
        #     # +1 for current move
        #     res = min(res, max(a, b) + 1)

        return res



# class Solution:
#     @cache
#     def superEggDrop(self, k: int, n: int) -> int:
#         # k eggs, n floors
#         # dp[i][j] = no. of drops for i eggs, j floors

#         # dp = [[math.inf] * (n + 1) for _ in range(k + 1)]
#         # for i in range(1, k + 1):
#         #     dp[i][0] = 0
#         #     dp[i][1] = 1
#         # for j in range(1, n + 1):
#         #     dp[1][j] = j
#         # # dp[i][j] = max(dp[i - 1, x - 1], dp[i, j - x])
#         # for i in range(2, k + 1):
#         #     # insight: optimal floor k increases as j increases
#         #     k = 1
#         #     for j in range(2, n + 1):
#         #         while k < j + 1 and dp[i][j - k] > dp[i - 1][k - 1]:
#         #             k += 1
#         #         dp[i][j] = 1 + dp[i - 1][k - 1]
#         # return dp[-1][-1]

#         # rephrase the problem as 
#         # dp[i][j] = max number of floors for i moves, j eggs

#         # dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + 1
#         # if jth egg break + dp[i-1][j-1]
#         # if jth egg doesn't break + dp[i-1][j]
#         # +current floor +1

#         dp = [0] * (k + 1)
#         m = 0
#         # iterate from 0 to m moves. stop when max number of floors covered for j eggs >= n.
#         while dp[k] < n:
#             m += 1
#             for i in range(k, 0, -1):
#                 dp[i] += dp[i - 1] + 1
#         return m
31875