Created
March 25, 2019 18:12
-
-
Save ykdojo/783d101e366e4703b2cf75ddb6b0fc03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helper = function(current, last, target) { | |
if (current == target) { | |
return 0; | |
} else if (current > target) { | |
return Infinity; | |
} | |
let option1 = last != current ? helper(current, current, target) + 1 : Infinity; | |
let option2 = last != 0 ? helper(current + last, last, target) + 1 : Infinity; | |
if (option1 < option2) { | |
return option1; | |
} else { | |
return option2; | |
} | |
}; | |
/** | |
* @param {number} n | |
* @return {number} | |
*/ | |
var minSteps = function(n) { | |
return helper(1, 0, n); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment