Make 1 (Part 3)
InterviewTime limit0.5sMemory limit1024 MB
Given N up to 10^18, find the minimum number of operations (divide by 3, divide by 2, or subtract 1) to reduce N to 1.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Recursion, Hash map
- Solved
- No attempts yet
Problem
For an integer , there are three allowed operations.
- If is divisible by 3, divide it by 3.
- If is divisible by 2, divide it by 2.
- Subtract 1 from .
Given an integer , use these three operations as needed to make 1. Print the minimum number of operations needed.
Input
The first line contains an integer with .
Output
Print the minimum number of operations on the first line.
Hint
For , you can make 1 in 3 operations: 10 → 9 → 3 → 1.