Coding Quiz
In this coding quiz, we will write a program to calculate the minimum number of operations to reduce a given integer n to 1 using Dynamic Programming.
You can choose one of the following three operations:
-
If
nis divisible by 3, divide it by 3. -
If
nis divisible by 2, divide it by 2. -
Subtract 1 from
n.
Craft a function that uses these three operations efficiently to find the minimum number of operations needed to reduce n to 1.
Write Code
def solution(n):
# Write your code here
return
Constraints
-
The input
nis an integer between 2 and 10,000 inclusive. -
The function must be implemented using dynamic programming.
Example Input/Output
- Input:
10 - Output:
3 - Explanation: You can reduce 10 to 9, then 3, and finally 1, in a total of 3 operations.
- Input:
6 - Output:
2 - Explanation: Reduce 6 to 3, then to 1, in a total of 2 operations.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.