Egg Drop With 2 Eggs and N Floors LeetCode solution in JavaScript

1
/** https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
2
* @param {number} n
3
* @return {number}
4
*/
5
var twoEggDrop = function (n) {
6
// Writing down strategy on example 2 we can observe following pattern:
7
// Drop at floor: 9 22 34 45 55 64 72 79 85 90 94 97 99 100
8
// Diff from prev: 13 12 11 10 9 8 7 6 5 4 3 2 1
9

10
// So we have hypothesis algorithm
11
// That is, `n` minus `d` until `result(n)` is smaller than `d`, where `d` start at 1 and increment by 1 for each iteration. If `result(n)` is 0, subtract 1, else return the result
12

13
let d = 1;
14

15
while (n > d) {
16
n -= d;
17
d++;
18
}
19

20
if (n == 0) {
21
d--;
22
}
23

24
return d;
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0