Problem Statement |
| There are three types of operations you can perform on an integer:
- 1. If it's divisible by 3, divide it by 3.
- 2. If it's divisible by 2, divide it by 2.
- 3. Subtract 1.
Given an int n, return the minimal number of operations needed to produce the number 1. |
|
Definition |
| Class: | DecreasingNumber | Method: | numberOfOperations | Parameters: | int | Returns: | int | Method signature: | int numberOfOperations(int n) | (be sure your method is public) |
|
|
|
|
Constraints |
- | n will be between 1 and 106, inclusive. |
|
Examples |
0) | |
| | Returns: 0 | We are already at number 1. |
|
|
1) | |
| |
2) | |
| | Returns: 3 | First, substract 1. Then, divide by 2 twice. (Or substract 1 twice, and then divide by 3). |
|
|
3) | |
| | Returns: 3 | First, substract 1. Then, divide by 3 twice. |
|
|
4) | |
| |