SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- public class EggDropBottomUp {
- public static int bottomUpEggDrop(int eggs, int floors) {
- int[][] dp = new int[eggs + 1][floors + 1];
- for (int egg = 1; egg < dp.length; egg++) {
- for (int floor = 1; floor < dp[0].length; floor++) {
- if (floor == 1) {
- dp[egg][floor] = 1;
- } else if (egg == 1) {
- dp[egg][floor] = floor;
- } else {
- int maxDrops = Integer.MAX_VALUE;
- for (int flAttempt = 1; flAttempt <= floor; flAttempt++) {
- int breaks = dp[egg-1][flAttempt-1];
- int doesntBreak = dp[egg][floor-flAttempt];
- maxDrops = Math.min(maxDrops, Math.max(breaks, doesntBreak));
- }
- dp[egg][floor] = maxDrops + 1;
- }
- }
- }
- return dp[eggs][floors];
- }
- }
RAW Paste Data

