Lecture
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.
Lessons in this chapter · Practical Python Algorithms
- 1. Advanced Python Algorithms
- 2. What is a Recursive Call?
- 3. Implementing Fibonacci Sequence with Recursive Function
- 4. Fill-in-the-blank quiz
- 5. Coding Quiz - Fibonacci Sequence
- 6. Dynamic Programming and Divide and Conquer
- 7. Implementing Dynamic Programming in Python
- 8. Multiple-choice quiz
- 9. Coding Quiz - Make One
- 10. What is Merge Sort?
- 11. Implementing Merge Sort
- 12. Multiple-choice quiz
- 13. Coding Quiz - Sort a List Using Merge Sort
- 14. What is Quick Sort?
- 15. Implementing Quick Sort in Python
- 16. Multiple-choice quiz
- 17. Coding Quiz - Sort a List Using Quick Sort
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help