Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing the Super Egg Drop Problem: A Dynamic Programming Approach #394

Open
algowhiz opened this issue Oct 27, 2024 · 0 comments
Open

Comments

@algowhiz
Copy link

#include
#include
#include <limits.h>

using namespace std;

int superEggDrop(int k, int n) {
vector<vector> ans(k + 1, vector(n + 1, 0));

for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
        if (i == 1) {
            ans[i][j] = j; 
        } else if (j == 1) {
            ans[i][j] = 1; 
        } else {
            int low = 1, high = j;
            while (low + 1 < high) {
                int mid = (low + high) / 2;

                int broken = ans[i - 1][mid - 1]; // Egg breaks
                int notBroken = ans[i][j - mid];   // Egg does not break

                if (broken > notBroken) {
                    high = mid;
                } else {
                    low = mid;
                }
            }

            // check both low and high
            ans[i][j] = 1 + min(max(ans[i - 1][low - 1], ans[i][j - low]),
                               max(ans[i - 1][high - 1], ans[i][j - high]));
        }
    }
}
return ans[k][n];

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant