Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. package com.java;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Objects;
  6.  
  7. public class EggsFloorsSecondAttempt {
  8. private static class EggDrops {
  9. private int eggs;
  10. private int drops;
  11.  
  12. public EggDrops(int eggs, int drops) {
  13. this.eggs = eggs;
  14. this.drops = drops;
  15. }
  16.  
  17. @Override
  18. public boolean equals(Object o) {
  19. if (this == o) return true;
  20. if (o == null || getClass() != o.getClass()) return false;
  21. EggsFloorsSecondAttempt.EggDrops eggDrops = (EggsFloorsSecondAttempt.EggDrops) o;
  22. return eggs == eggDrops.eggs &&
  23. drops == eggDrops.drops;
  24. }
  25.  
  26. @Override
  27. public int hashCode() {
  28. return Objects.hash(eggs, drops);
  29. }
  30. }
  31.  
  32.  
  33. private static Map<EggsFloorsSecondAttempt.EggDrops, Integer> floorCache = new HashMap<>();
  34.  
  35. public static int getNumberFloors(int eggs, int drops) {
  36. if(eggs <= 0 || drops <= 0) {
  37. return 0;
  38. }
  39. if(eggs == 1) {
  40. return drops;
  41. }
  42. if(drops == 1) {
  43. return 1;
  44. }
  45.  
  46. EggsFloorsSecondAttempt.EggDrops eggDrops = new EggsFloorsSecondAttempt.EggDrops(eggs, drops);
  47. if(floorCache.containsKey(eggDrops)) {
  48. return floorCache.get(eggDrops);
  49. }
  50.  
  51. int result = getNumberFloors(eggs - 1, drops - 1) + 1 + getNumberFloors(eggs, drops - 1);
  52. floorCache.put(eggDrops, result);
  53. return result;
  54. }
  55.  
  56. public static int getMinimumEggDrops(int eggs, int floors) {
  57. for(int i = 1; i <= floors; ++i) {
  58. if (getNumberFloors(eggs, i) >= floors) return i;
  59. }
  60. throw new RuntimeException("Something is wrong here!");
  61. }
  62.  
  63.  
  64. public static int getFirstDroppingFloor(int eggs, int floors) {
  65. int lo = 1;
  66. int hi = floors;
  67. int s;
  68. int s_min = 0;
  69. int wc = Integer.MAX_VALUE;
  70. while(lo < hi){
  71. s = (lo + hi) / 2;
  72. int b = getMinimumEggDrops(eggs - 1, s - 1);
  73. int nb = getMinimumEggDrops(eggs, floors - s);
  74.  
  75.  
  76. int wc1 = 1 + Math.max(b, nb);
  77. if(wc1 < wc) {
  78. wc = wc1;
  79. s_min = s;
  80. }
  81. if(b == nb) break;
  82. else if(b > nb) {
  83. hi = s - 1;
  84. }
  85. else {
  86. lo = s + 1;
  87. }
  88. }
  89. return s_min;
  90. }
  91.  
  92. public static void main(String[] args) {
  93. System.out.println(getMinimumEggDrops(2, 100)); // prints 14 correct
  94. System.out.println(getMinimumEggDrops(3, 100)); // prints 9 correct
  95. System.out.println(getFirstDroppingFloor(2, 36)); // prints 9 wrong should be 8 or 14
  96. System.out.println(getFirstDroppingFloor(3, 100)); // prints 25
  97.  
  98. }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement