<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 {
    map<pair<int, int>, int> memo;
public:
    int superEggDrop(int k, int n) {
        // if I only have 1 egg, then my only strategy is to
        // drop at floor 0, 1, 2, ... n-1 until it breaks
        // (if it doesn't break at n-1, then the answer is definitely n)
        if (k == 1) {
            return n;
        }
        // if I have 1 floor left, then I drop an egg on the 0th floor and know the answer
        // if I have 0 floors left, I don't even need to drop an egg to know the answer
        if (n <= 1) {
            return n;
        }

        if (memo.contains({k, n})) {
            return memo[{k, n}];
        }
        
        int l = 0; 
        int r = n;
        while (l < r) {
            // we'll try to drop it from the mid-th floor
            int mid = (l + r) / 2;
            // if it survives, then we keep our eggs and only consider floors mid+1, mid+2, .. n
            int surviveResult = 1 + superEggDrop(k, n - (mid + 1));
            // if it breaks, then we lose one egg, and only consider floors 0,1,2, .. mid
            int breakResult = 1 + superEggDrop(k - 1, mid); 

            // we want to get the nice middle value
            if (surviveResult == breakResult) {
                l = mid;
                break;
            } else if (surviveResult < breakResult) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        int best = 1 + max(superEggDrop(k, n - (l + 1)), superEggDrop(k - 1, l));
        memo[{k, n}] = best;
        return best;
    }
};
25000