-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patheggDrop.js
More file actions
109 lines (48 loc) · 1.2 KB
/
eggDrop.js
File metadata and controls
109 lines (48 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// given two eggs and a building that is 100 stories tall,
// what is the fewest number of steps to find how high an
// egg can be dropped safely from?
//
var eggDrop = function(safeHeight){
}
// max number of egg drops necessary to find worse-case
// scenario is 19 drops for a safe height of 99 stories
var eggDropTens = function(safe){
var counter = 0;
var height = 0;
while (height <= safe){
height += 10;
counter++;
}
if (height === 110){
return 10;
}
height -= 9;
while (height <= safe+1){
counter++;
height += 1;
}
return ((safe+1) % 10 === 0)? counter-1 : counter;
}
var eggDropEff = function(safe){
var counter = 0;
var steps = 15;
var height = 0;
while (height <= safe){
height += steps;
counter++;
steps--;
}
steps++;
height -= (steps-1);
while (height <= safe+1){
counter++;
height += 1;
}
// console.log("COUNTER: ", counter);
// console.log("HEIGHT: ", height);
// console.log("steps: ", steps);
return (height === safe-1)? counter-1 : counter;
}
for (var i = 0; i < 101; i++){
console.log("Safe Height: ", i, "Attempts Necessary: ", eggDropEff(i));
}