<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:
    def superEggDrop(self, k: int, n: int) -> int:
        """
        # math
        # O(KlogN)   O(1)
        def f(x):
            res = 0
            r = 1
            for i in range(1, k+1):
                r *= x-i+1
                r //= i
                res += r
                if res > =n: break
            return res

        left, right = 1, n
        while left <= right:
            mid = (left + right) // 2
            if f(mid) < n:
                left = mid + 1
            else:
                right = mid - 1
        return left
        """





        # finding the floor which minimizes the steps
        # dp(k,n) = 1 + min(max(dp(k-1, X-1), dp(k, n-X))) # max(broken case, not broken case)
        # top-donw dp : trade off
        # O(KNlogN) O(KN)
        # check bottom-up and math approaches later!!!
        @cache
        def dp(n, k):
            if k == 1 or n == 1 or n == 0: # only 1 egg => n check from the bottom check each floor by moving upper
                return n ##### returing n as 

            res = inf
            """
            # O(KN^2)
            # understand this first
            for i in range(1, n):
                # two cases => 1) broken 2) not broken
                broken = dp(k-1,i-1) # broken in mid floor => checking lower floors
                exist = dp(k,n-i) # not broken in mid floor => checking higher floors
                res = min(res, 1 + max(broken, exist))
            return res
            """
            left, right = 0, n 
            while left <= right:
                mid = (left + right) // 2 # assuming the mid is the floor which needs min steps
                # we may have two cases in the floor "mid": 1) broken or 2) exist
                broken = dp(mid-1, k-1) # broken in mid floor => checking lower floors # increasing function on mid (X)
                exist = dp(n-mid, k) # not broken in mid floor => checking higher floors # descreasing function on mid(X)
                
                if broken >= exist: # finding the top floor which broken first 
                    right = mid-1
                else:
                    left = mid+1
                    
                res = min(res, 1 + max(broken, exist)) # each range: tracking the min steps # trading off
            
            return res       
   
        return dp(n, k)
    



38456