Skip to content

Instantly share code, notes, and snippets.

@eclipselu
Created September 13, 2016 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eclipselu/ac2858b93fee576bbbdf5594450d7b68 to your computer and use it in GitHub Desktop.
Save eclipselu/ac2858b93fee576bbbdf5594450d7b68 to your computer and use it in GitHub Desktop.
Integer Replacement
class Solution {
public:
int integerReplacement(int n) {
return helper(n);
}
private:
int helper(long n) {
if (n == 1)
return 0;
if (n % 2 == 0)
return 1 + helper(n / 2);
else
return 1 + min(helper(n - 1), helper(n + 1));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment