Skip to content

Instantly share code, notes, and snippets.

@ykdojo
Created March 25, 2019 18:12
Show Gist options
  • Save ykdojo/783d101e366e4703b2cf75ddb6b0fc03 to your computer and use it in GitHub Desktop.
Save ykdojo/783d101e366e4703b2cf75ddb6b0fc03 to your computer and use it in GitHub Desktop.
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