daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class EggDropBottomUp {
  2.     public static int bottomUpEggDrop(int eggs, int floors) {
  3.         int[][] dp = new int[eggs + 1][floors + 1];
  4.         for (int egg = 1; egg < dp.length; egg++) {
  5.             for (int floor = 1; floor < dp[0].length; floor++) {
  6.                 if (floor == 1) {
  7.                     dp[egg][floor] = 1;
  8.                 } else if (egg == 1) {
  9.                     dp[egg][floor] = floor;
  10.                 } else {
  11.                     int maxDrops = Integer.MAX_VALUE;
  12.                     for (int flAttempt = 1; flAttempt <= floor; flAttempt++) {
  13.                         int breaks = dp[egg-1][flAttempt-1];
  14.                         int doesntBreak = dp[egg][floor-flAttempt];
  15.                         maxDrops = Math.min(maxDrops, Math.max(breaks, doesntBreak));
  16.                     }
  17.                     dp[egg][floor] = maxDrops + 1;
  18.                 }
  19.             }
  20.         }
  21.         return dp[eggs][floors];
  22.     }
  23. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top