Advertisement
Guest User

Untitled

a guest
Feb 1st, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class EggsFloors {
  4. private static class EggDrops {
  5. private int eggs;
  6. private int drops;
  7.  
  8. public EggDrops(int eggs, int drops) {
  9. this.eggs = eggs;
  10. this.drops = drops;
  11. }
  12.  
  13. @Override
  14. public boolean equals(Object o) {
  15. if (this == o) return true;
  16. if (o == null || getClass() != o.getClass()) return false;
  17. EggDrops eggDrops = (EggDrops) o;
  18. return eggs == eggDrops.eggs &&
  19. drops == eggDrops.drops;
  20. }
  21.  
  22. @Override
  23. public int hashCode() {
  24. return Objects.hash(eggs, drops);
  25. }
  26. }
  27.  
  28.  
  29. private static Map<EggDrops, Integer> floorCache = new HashMap<>();
  30.  
  31. public static int getNumberFloors(int eggs, int drops) {
  32. if(eggs <= 0 || drops <= 0) {
  33. return 0;
  34. }
  35. if(eggs == 1) {
  36. return drops;
  37. }
  38. if(drops == 1) {
  39. return 1;
  40. }
  41.  
  42. EggDrops eggDrops = new EggDrops(eggs, drops);
  43. if(floorCache.containsKey(eggDrops)) {
  44. return floorCache.get(eggDrops);
  45. }
  46.  
  47. int result = getNumberFloors(eggs - 1, drops - 1) + 1 + getNumberFloors(eggs, drops - 1);
  48. floorCache.put(eggDrops, result);
  49. return result;
  50. }
  51.  
  52. public static int getMinimumEggDrops(int eggs, int floors) {
  53. for(int i = 1; i <= 200; i++) {
  54. getNumberFloors(eggs, i);
  55. }
  56.  
  57. int min = Integer.MAX_VALUE;
  58. int drops = 0;
  59. for(Map.Entry<EggDrops, Integer> entry: floorCache.entrySet()) {
  60. if(entry.getKey().eggs != eggs) continue;
  61. if(entry.getValue() < min && entry.getValue() >= floors && drops < entry.getValue()) {
  62. min = entry.getValue();
  63. drops = entry.getKey().drops;
  64. }
  65. }
  66. return drops;
  67. }
  68.  
  69. public static int getWorseCaseDropsForBreaking(int startFloor, int eggs, int floors) {
  70. return 1 + Math.max(getMinimumEggDrops(eggs - 1, startFloor - 1), getMinimumEggDrops(eggs, floors - startFloor));
  71. }
  72.  
  73. public static int getFirstBreakingFloor(int eggs, int floors) {
  74. int minDrops = Integer.MAX_VALUE;
  75. int firstBreakingFloor = Integer.MAX_VALUE;
  76. for(int s = 1; s <= floors; s++) {
  77. int worseCaseDrops = getWorseCaseDropsForBreaking(s, eggs, floors);
  78. if(minDrops > worseCaseDrops) {
  79. minDrops = worseCaseDrops;
  80. firstBreakingFloor = s;
  81. }
  82. }
  83. return firstBreakingFloor;
  84. }
  85. public static void main(String[] args) {
  86. System.out.println(getMinimumEggDrops(2, 100)); // prints 14 correct
  87. System.out.println(getMinimumEggDrops(3, 100)); // prints 9 correct
  88. System.out.println(getFirstBreakingFloor(2, 36)); // prints 33 wrong
  89. System.out.println(getFirstBreakingFloor(3, 100)); // prints 8 wrong
  90. }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement