Problem Statement
- "i" - increment the value,
- "d" - decrement the value,
- "s" - square the value, and
- "p" - sort the digits of the number into non-increasing order (i.e., biggest to smallest).
For example, "p" changes 4070 to 7400, and it changes -4070 to -7400 (the minus sign is preserved).
You are given an
Definition
- Class:
- Deadfish
- Method:
- shortestCode
- Parameters:
- int
- Returns:
- int
- Method signature:
- int shortestCode(int N)
- (be sure your method is public)
Constraints
- N will be between 1 and 200,000, inclusive.
Examples
3
Returns: 3
The fastest way to get to 3 is "iii" - three increments.
5
Returns: 4
One fastest way to get a 5 is "iisi": two increments to get a 2, square to get a 4 and one more increment. Another optimal way is "ddsi": two decrements change the register to -2, squaring that gives 4, and the final increment changes it to 5.
15
Returns: 5
The fastest way to get 15 is "iissd". The value in the register will change as follows: 0, 1, 2, 4, 16, 15.
61
Returns: 5
The fastest way to get 61 is "iissp". The value in the register will change as follows: 0, 1, 2, 4, 16, 61.
200000
Returns: 207
199363
Returns: 462
max test
72
Returns: 7
32
Returns: 8
50
Returns: 7
90
Returns: 9
27
Returns: 7
6
Returns: 5
43
Returns: 8
16
Returns: 4
8
Returns: 5
447
Returns: 15
743
Returns: 13
487
Returns: 12
813
Returns: 13
220
Returns: 10
854
Returns: 10
124
Returns: 10
930
Returns: 12
325
Returns: 8
553
Returns: 8
5898
Returns: 41
4161
Returns: 49
1821
Returns: 37
4204
Returns: 14
9012
Returns: 22
4364
Returns: 17
7598
Returns: 16
1546
Returns: 35
2241
Returns: 32
5567
Returns: 27
11845
Returns: 46
27035
Returns: 153
41936
Returns: 79
37210
Returns: 50
43002
Returns: 18
25149
Returns: 151
47712
Returns: 201
74339
Returns: 23
88257
Returns: 50
92879
Returns: 143
161456
Returns: 160
154845
Returns: 407
171731
Returns: 351
162069
Returns: 353
116630
Returns: 354
145491
Returns: 359
137272
Returns: 388
40595
Returns: 207
131073
Returns: 39
199487
Returns: 338
5555
Returns: 15
199999
Returns: 206
159678
Returns: 332
14
Returns: 6
78965
Returns: 19
49729
Returns: 9
166788
Returns: 338
111321
Returns: 236
66553
Returns: 7
3362
Returns: 11
141334
Returns: 66
30
Returns: 9
225
Returns: 6
87
Returns: 9
60
Returns: 6
139
Returns: 13
421
Returns: 9
59
Returns: 7