leetcode 887. 鸡蛋掉落

O(knlogn)
class Solution {
public:
    int mp[110][11111];
    int dp(int k,int n)
    {
        if(k==1) return n;
        if(n==0) return 0;
        //if(mp[make_pair(k,n)]) return mp[make_pair(k,n)];
        if(mp[k][n]) return mp[k][n];
        int res=10000000;
        int l=1;
        int r=n;
        while(l <= r)
        {
            int mid=(l+r)/2;
            int broken=dp(k-1,mid-1);
            int unbroken=dp(k,n-mid);
            if(broken > unbroken)
            {
                r=mid-1;
                res=min(res,broken+1);
            }
            else 
            {
                l=mid+1;
                res=min(res,unbroken+1);
            }
        }
       // mp[make_pair(k,n)] = res;
        mp[k][n]=res;
        return res;
    }
    int superEggDrop(int K, int N) {
       int ans=dp(K,N);
       return ans;
    }
};

 

你可能感兴趣的:(Leetcode,复习)