[Programmers / JAVA] Level 1 Pressing the Keypad (67256)
[Programmers / JAVA] Level 1 Pressing the Keypad (67256)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
Each key on a smartphone's phone keypad has a number written on it as follows.
On this phone keypad, you're going to enter only digits using just the thumbs of your left and right hands. The left thumb starts at the * key and the right thumb starts at the # key, and the rules for using the thumbs are as follows.
- A thumb can only move in the four directions: up, down, left, and right, and moving one square on the keypad counts as a distance of 1.
- To press one of the three numbers 1, 4, 7 in the left column, use the left thumb.
- To press one of the three numbers 3, 6, 9 in the right column, use the right thumb.
- To press one of the four numbers 2, 5, 8, 0 in the middle column, use whichever thumb is currently closer to that key.
- If the two thumbs are the same distance away, right-handed people use the right thumb, and left-handed people use the left thumb.
Given an array numbers containing the numbers to press in order, and a string hand indicating whether the person is left-handed or right-handed, as parameters, complete the solution function so that it returns a continuous string indicating whether the left or right thumb was used to press each number.
- The size of the numbers array is between 1 and 1,000 inclusive.
- Each element of the numbers array is an integer between 0 and 9 inclusive.
- hand is either "left" or "right".
- "left" means left-handed, and "right" means right-handed.
- Return a string formed by appending L when the left thumb is used and R when the right thumb is used, in order.
| numbers | hand | result |
|---|---|---|
| [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] | "right" | "LRLLLRLLRRL" |
| [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] | "left" | "LRLLRRLLLRR" |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] | "right" | "LLRLLRLLRL" |
Input/Output Example #1
The numbers to press in order are [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5], and the person is right-handed.
| Left hand position | Right hand position | Number to press | Hand used | Explanation |
|---|---|---|---|---|
| * | # | 1 | L | 1 is pressed with the left hand. |
| 1 | # | 3 | R | 3 is pressed with the right hand. |
| 1 | 3 | 4 | L | 4 is pressed with the left hand. |
| 4 | 3 | 5 | L | The left-hand distance is 1 and the right-hand distance is 2, so 5 is pressed with the left hand. |
| 5 | 3 | 8 | L | The left-hand distance is 1 and the right-hand distance is 3, so 8 is pressed with the left hand. |
| 8 | 3 | 2 | R | The left-hand distance is 2 and the right-hand distance is 1, so 2 is pressed with the right hand. |
| 8 | 2 | 1 | L | 1 is pressed with the left hand. |
| 1 | 2 | 4 | L | 4 is pressed with the left hand. |
| 4 | 2 | 5 | R | Since the left-hand distance and right-hand distance are equal at 1, 5 is pressed with the right hand. |
| 4 | 5 | 9 | R | 9 is pressed with the right hand. |
| 4 | 9 | 5 | L | The left-hand distance is 1 and the right-hand distance is 2, so 5 is pressed with the left hand. |
| 5 | 9 | - | - |
Therefore, LRLLLRLLRRL is returned.
Input/Output Example #2
If a left-handed person presses [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] in order, the hand used becomes LRLLRRLLLRR.
Input/Output Example #3
If a right-handed person presses [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] in order, the hand used becomes LLRLLRLLRL.
Since 1, 4, 7 use the left hand and 3, 6, 9 use the right hand, there's no real difficulty there, but for 2, 5, 8, 0 you need to use whichever hand is currently positioned closest to the number to press.
In other words, each time a number is pressed, record the positions of the left and right hands, and when one of 2, 5, 8, 0 comes up, compute and compare the distance from each hand's recorded position to that number.
To model the keypad, build a 2D array.
JAVA
private static final int[][] KEYPAD = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { -2, 0, -1 } }; private int left = -2; private int right = -1;
Treat the keypad's special characters * and # as -2 and -1 respectively.
Per the problem's conditions, the initial position of each hand is the left hand at * and the right hand at #, so the initial values are likewise set to -2 and -1.
Build a method that can compute the position on the keypad.
JAVA
private int[] getPosition(int number) { int[] pos = new int[] { 0, 0 }; for (int i = 0; i < KEYPAD.length; i++) { for (int j = 0; j < KEYPAD[i].length; j++) { if (KEYPAD[i][j] == number) { pos[0] = i; pos[1] = j; } } } return pos; }
This traverses the two-dimensional array and returns the array index of the element whose value matches the given keypad number number.
For example, keypad 5 has the position (1, 1), and keypad 7 has the position (2, 0).
We can see that the distance between keypad 5 and 7 is |1 - 2| + |1 - 0| = 2.
JAVA
private int left = -2; private int right = -1; int[] lPos = getPosition(left); int[] rPos = getPosition(right); int[] toPos = getPosition(number); int lLength = Math.abs(lPos[0] - toPos[0]) + Math.abs(lPos[1] - toPos[1]); int rLength = Math.abs(rPos[0] - toPos[0]) + Math.abs(rPos[1] - toPos[1]); // If the left thumb is closer if (lLength > rLength) { // Move left hand } // If the right thumb is closer else if (lLength < rLength) { // Move right hand } // If both are the same else { // If left-handed if (hand.equals("left")) { // Move left hand } // If right-handed else { // Move right hand } }
If a number belonging to 2, 5, 8, 0 comes in, it can be implemented as shown above.
lPos and rPos are the positions of the left and right hands, and toPos is the position of the key that needs to be reached.
Using this, compute the distances from each hand to the key, lLength and rLength, and compare them.
JAVA
/** * Pressing the Keypad class * * @author RWB * @since 2021.12.09 Thu 18:58:49 */ class Solution { private static final int[][] KEYPAD = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { -2, 0, -1 } }; private int left = -2; private int right = -1; /** * Method that returns the answer * * @param numbers: [int[]] The numbers to press in order * @param hand: [String] Dominant hand (left, right) * * @return [String] The answer */ public String solution(int[] numbers, String hand) { StringBuilder builder = new StringBuilder(); for (int number : numbers) { // If it's a left-column number if (number == 1 || number == 4 || number == 7) { left = number; builder.append("L"); } // If it's a right-column number else if (number == 3 || number == 6 || number == 9) { right = number; builder.append("R"); } // If it's a middle-column number else { int[] lPos = getPosition(left); int[] rPos = getPosition(right); int[] toPos = getPosition(number); int lLength = Math.abs(lPos[0] - toPos[0]) + Math.abs(lPos[1] - toPos[1]); int rLength = Math.abs(rPos[0] - toPos[0]) + Math.abs(rPos[1] - toPos[1]); // If the left thumb is closer if (lLength > rLength) { right = number; builder.append("R"); } // If the right thumb is closer else if (lLength < rLength) { left = number; builder.append("L"); } // If both are the same else { // If left-handed if (hand.equals("left")) { left = number; builder.append("L"); } // If right-handed else { right = number; builder.append("R"); } } } } return builder.toString(); } /** * Method that returns the position * * @param number: [int] The key * * @return [int[]] The position */ private int[] getPosition(int number) { int[] pos = new int[] { 0, 0 }; for (int i = 0; i < KEYPAD.length; i++) { for (int j = 0; j < KEYPAD[i].length; j++) { if (KEYPAD[i][j] == number) { pos[0] = i; pos[1] = j; } } } return pos; } }
That's the code.
