<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 {
    unordered_map<int,int> memo;

    int dp(int k, int n) {
        int key = n*100+k;
        if(memo.count(key)){
            return memo[key];
        }

        if (k==1||n==0){
            return memo[key]=n;
        }

        int left = 1, right = n;

        while(left<=right){
            int x = left+(right-left>>1);
            int x1 = dp(k-1,x-1);
            int x2 = dp(k, n-x);

            if(x1==x2){
                left=right=x;
                break;
            } else if (x1<x2){
                left = x+1;
            } else {
                right = x-1;
            }
        }

        return memo[key] = min(max(dp(k-1,left-1), dp(k,n-left)),max(dp(k-1,right-1),dp(k,n-right)))+1;
    }

    int dp2O(int k, int n){
        if(n==1) {
            return 1;
        }

        vector<int> dp(k+1);
        dp[1] = 1;

        for(int i=2; i<=n; ++i){
            for(int j=k; j>1; --j){
                dp[j] = j==1? i : dp[j-1]+dp[j]+1;
            }

            if (dp[k]>=n){
                return i;
            }
        }

        return -1;
    }

    int dp2(int k, int n){
        if(n==1) {
            return 1;
        }

        vector<vector<int>> dp(n+1, vector<int>(k+1));
        for(int j=1; j<=k; ++j){
            dp[1][j] = 1;
        }

        for(int i=1; i<=n; ++i){
            dp[i][1] = i;
        }

        for(int i=2; i<=n; ++i){
            for(int j=2; j<=k; ++j){
                dp[i][j] = dp[i-1][j-1]+dp[i-1][j]+1;
            }

            if (dp[i][k]>=n){
                return i;
            }
        }

        return -1;
    }

public:
    int superEggDrop(int k, int n) {
        // return dp2O(k,n);

        // return dp2(k,n);

        return dp(k,n);
    }
};

23800