Problem Statement
Often, I find myself trying to repeat a certain character a great number of times in a primitive editor. For example, on many occasions I have had to make 50 periods. To type in a single character 50 times is very tedious, so I usually do something like type in the character 5 times, then copy those 5 occurrences of the character, and paste 5 characters at a time until I have 50. This might not be the best way to get 50 though, so in this problem, your task is to determine the optimal way to get a certain number of characters. You may use only the following keys:
For example, the sequence "....DHUCVVB" will perform the following:
First, we type 4 characters. Next we depress the shift key, and press the home key, and release the shift key, selecting all four characters. We now copy the four characters and paste twice, so there are 12 characters and the cursor is between the 8th and 9th characters. Finally, we press backspace to delete one of them, leaving us with 11 characters in 9 keystrokes (remember not to count 'U' and 'D').
Your task is to compute and return the minimum number of keystrokes required to create exactly n occurrences of a character. Thus, in the above example, we got 11 characters in 9 keystrokes, and since it turns out that there is no way to get 11 characters in less than 9 keystrokes, we would return 9 if n were 11.
- The character to be repeated, which will be represented by a period ('.') in this problem.
- The arrow keys, which allow you to move the cursor left or right. These will be represented by 'L' and 'R', respectively.
- The home and end keys, which move to the beginning and end of the string of characters entered so far, respectively. These will be represented by 'H' and 'E', respectively.
- The shift key which, when used in conjunction with the arrow keys or the home and end keys, allows you to select characters. We will represent depressing the shift key by 'D', and releasing it by 'U'. However, we will not count pressing or releasing the shift key as a keystroke, since that can be done without much added trouble.
- The key combinations Ctrl-C and Ctrl-V, which allow you to copy and paste text, respectively. These will simply be represented by 'C' and 'V', respectively, and only count as a single keystroke each.
- The backspace key allows you to delete one character immediately before (to the left of) the cursor, each time it is pressed. This will be represented by 'B'.
For example, the sequence "....DHUCVVB" will perform the following:
First, we type 4 characters. Next we depress the shift key, and press the home key, and release the shift key, selecting all four characters. We now copy the four characters and paste twice, so there are 12 characters and the cursor is between the 8th and 9th characters. Finally, we press backspace to delete one of them, leaving us with 11 characters in 9 keystrokes (remember not to count 'U' and 'D').
Your task is to compute and return the minimum number of keystrokes required to create exactly n occurrences of a character. Thus, in the above example, we got 11 characters in 9 keystrokes, and since it turns out that there is no way to get 11 characters in less than 9 keystrokes, we would return 9 if n were 11.
Definition
- Class:
- FewestStrokes
- Method:
- getStrategy
- Parameters:
- int
- Returns:
- int
- Method signature:
- int getStrategy(int n)
- (be sure your method is public)
Notes
- Pressing left or backspace at the far left of the sequence or right at the far right of the sequence deselects anything that is selected, but has no other effect.
- The cursor position is always between two adjacent characters or at the beginning or end of the line, not on top of a character (as in some editors).
- It turns out that, for the given input range, the largest return is less than or equal to 30.
Constraints
- n will be between 1 and 1,000, inclusive.
Examples
10
Returns: 8
One way to do this in 8 keystrokes is "...DHUCVV." In other words, type three characters, copy them, and then paste twice, and insert a final character.23
Returns: 12
We can get 24 characters with 11 keystrokes and then press the backspace key: "......DHUCVVVB"58
Returns: 16
This can be done as: "...DHUCVVVVVVDHUCVV." In other words, insert 3 characters, copy them and paste 6 times to get 21 characters. Then, select 18 of the 21, paste them twice and add a final '.'41
Returns: 14
We can do this as: "...DHUCVVVVRDHUCVV"989
Returns: 27
This is a tricky one: "....DHUCVVVVHDELUCVVVDHUCVVVDHUCVVV"994
Returns: 28
We can do this one as: "...DHUCVVHDEUCVVDHUCVVVVVVDHRUCVVVVV"1
Returns: 1