TopCoder problem "DecreasingNumber" used in TCHS SRM 45 (Division I Level Two)



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)
    
1
Returns: 0
We are already at number 1.
1)
    
2
Returns: 1
Only divide it by 2.
2)
    
5
Returns: 3
First, substract 1. Then, divide by 2 twice. (Or substract 1 twice, and then divide by 3).
3)
    
10
Returns: 3
First, substract 1. Then, divide by 3 twice.
4)
    
962340
Returns: 17

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8096

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10799&pm=8096

Writer:

Relja

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Dynamic Programming, Search