Joystick (with.Java)
This is an article about the joystick (with.Java) problem.
I would like to solve the coding test problem and reflect on the problem I solved.
Letβs get to the problem first.
Problem
Complete the alphabet names with joysticks.
At first, it only consists of A.
ex. The name that needs to be completed is AAA if itβs three letters, and AAA if itβs four letters
If you move the joystick in each direction, it looks like below.
β² - Next alphabet
βΌ - Previous alphabet (move down from A to Z)
β - Move the cursor to the left (if you move to the left from the first position, the cursor to the last character)
βΆ - Move cursor to the right (if you move to the right from the last position, cursor to the first character)
For example, you can create a βJAZβ in the following way.
- In the first position, operate the joystick up 9 times to complete J.
- Move the cursor to the last character position by manipulating the joystick to the left once.
- In the last position, operate the joystick down once to complete Z.
So you can move 11 times to make βJAZβ, which is the minimum movement.
If the name you want to create is given as a parameter, create a solution function to return the minimum number of joystick operations for the name.
Restrictions
- The name consists only of uppercase letters.
- The length of the name is 1 or more and 20 or less.
Input/Output Examples
name | return |
---|---|
βJEROENβ | 56 |
βJANβ | 23 |
my solution to the problem
class Solution {
public int solution(String name) {
int answer = 0;
int cursor = name.length() - 1;
for(int i = 0; i < name.length(); i++){
answer += Math.min(name.charAt(i) - 'A', 'Z' - name.charAt(i) + 1);
if(i < name.length() - 1 && name.charAt(i + 1) == 'A'){
int temp = i + 1;
while(temp < name.length() && name.charAt(temp) == 'A'){
temp++;
}
cursor = Math.min(cursor, (i * 2) + (name.length() - temp));
cursor = Math.min(cursor, i + (name.length() - temp) * 2);
}
}
return answer + cursor;
}
}
Solution Description
First, the answer variable is a variable that accumulates the number of manipulations of a string that must be created by manipulating each character.
Examine each character by repeating the string from start to finish.
Choose a smaller value between the distance from the current character βAβ and the distance from the current character βZβ to the current character to calculate the minimum number of operations required to manipulate the character.
For example, if the current character is βBβ, you can manipulate βAβ up to one space, so one manipulation is required.
Alternatively, you can operate one space down from βYβ to βZβ, so you need one operation.
In this way, each character calculates the number of operations and accumulates them in the answer variable.
Next, if you meet successive βAβ, calculate the additional number of moves.
If you encounter the part leading to βAβ, you should consider the additional number of operations as you move next.
This is because when you meet consecutive βAβ, the number of manipulations will also occur when you go back.
Therefore, if you encounter a continuous βAβ after the current character, you calculate the number of additional operations until the next move.
Use the cursor variable for this.
The cursor variable stores the minimum number of additional operations when meeting successive βAβ and moving on.
To initialize this, initialize the cursor to the length of the name -1.
This is because we assume that one move to the end of the string is the worst case.
If you meet successive βAβ, calculate the number of operations until the next move.
At this time, compare the two cases and choose a smaller value.
Move from the current character position i to the position where the successive βAβ ends, then add the distance from the end to the current position and the distance to the current position.
Alternatively, add the distance to the current position and the distance from the position where the successive βAβ ends to the end and then back again.
In this case, the distance to the current position is doubled by the distance from the position where the successive βAβ ends to the end.
Save the additional number of movements calculated in this way in the cursor variable.
Finally, add the accumulated number of text operations to the answer and the additional number of movements (cursor) to return the final number of operations.
Conclusion
In this way, we calculate the minimum number of operations to manipulate a given string to make it a target string.