leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0397.cpp (374B)


      1 class Solution {
      2   private:
      3     unordered_map<int, int> visited;
      4 
      5   public:
      6     int integerReplacement(int n) {
      7         if (n == 1) return 0;
      8         if (visited.count(n)) return visited[n];
      9 
     10         if (!(n & 1 == 1)) return visited[n] = 1 + integerReplacement(n / 2);
     11         return visited[n] = 2 + min(integerReplacement(n / 2), integerReplacement(n / 2 + 1));
     12     }
     13 };