-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.js
More file actions
23 lines (20 loc) · 455 Bytes
/
index.js
File metadata and controls
23 lines (20 loc) · 455 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'use strict';
module.exports = { minSteps };
/**
* @param {number} n
* @return {number}
*/
function minSteps(n) {
return n <= 1 ? 0 : getTotal(1, 1, 1);
function getTotal(current, copy, total) {
if (n < current) {
return Infinity;
}
if (n === current) {
return total;
}
const t1 = getTotal(current + copy, copy, ++total);
const t2 = getTotal(current * 2, current, ++total);
return Math.min(t1, t2);
}
}