[Lv.1]키패드 누르기

Jihyun-Jeon·2022년 3월 11일
0

문제: https://programmers.co.kr/learn/courses/30/lessons/67256

🔶 못품(2022.03.11)

  • 못 푼 이유
    keypad = [[1,2,3],[4,5,6],[7,8,9],[*,0,#]]
    이렇게 keypad배열을 두고 해당값의 배열번호와 인덱스 번호를 구하여 가로,세로 길이를 구하려 했으나 엄청난 for문을 돌아야해서 꼬여버림.

🔶다른 사람 풀이

// <방법1>- keypad의 각 번호의 인덱스 번호를 아예 객체로 다 만들어 놓고 시작함.
const solution = (numbers, hand) => {
 const answer = [];

 let leftHandPosition = '*';
 let rightHandPosition = '#';

 numbers.forEach((number) => {
   if (number === 1 || number === 4 || number === 7) {
     answer.push('L');
     leftHandPosition = number;
     return;
   }

   if (number === 3 || number === 6 || number === 9) {
     answer.push('R');
     rightHandPosition = number;
     return;
   }

   const leftHandDistance = getDistance(leftHandPosition, number);
   const rightHandDistance = getDistance(rightHandPosition, number);

   if (leftHandDistance === rightHandDistance) {
     if (hand === 'right') {
       answer.push('R');
       rightHandPosition = number;
       return;
     }

     if (hand === 'left') {
       answer.push('L');
       leftHandPosition = number;
       return;
     }
   }

   if (leftHandDistance > rightHandDistance) {
     answer.push('R');
     rightHandPosition = number;
   }

   if (leftHandDistance < rightHandDistance) {
     answer.push('L');
     leftHandPosition = number;
   }
 });

 return answer.join('');
};
// 가로,세로 거리구하는 함수
const getDistance = (handPosition, target) => {
 const keyPad = {
   1: [0, 0],
   2: [0, 1],
   3: [0, 2],
   4: [1, 0],
   5: [1, 1],
   6: [1, 2],
   7: [2, 0],
   8: [2, 1],
   9: [2, 2],
   '*': [3, 0],
   0: [3, 1],
   '#': [3, 2],
 };

 const nowPosition = keyPad[handPosition]; // [3,0] [3,2]
 const targetPosition = keyPad[target]; // [0,0]

 return (
   Math.abs(targetPosition[0] - nowPosition[0]) +
   Math.abs(targetPosition[1] - nowPosition[1])
 );
};
//
/* <방법2> - set,map, 이차원배열 이용 */
function solution(numbers, hand) {
 const hands = new Map();
 let answer = '';
 hands.set('left', '*');
 hands.set('right', '#');

 numbers.forEach((number) => {
   if ([1, 4, 7].includes(number)) {
     answer += 'L';
     hands.set('left', number);
   } else if ([3, 6, 9].includes(number)) {
     answer += 'R';
     hands.set('right', number);
   } else {
     answer += findNearest(hands, number, hand);
   }
 });

 return answer;
}
// 거리구하는 함수
function findNearest(hands, number, hand) {
 const keypad = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9],
   ['*', 0, '#'],
 ];

 const left = hands.get('left');
 const right = hands.get('right');
 let numIndex;
 let leftIndex;
 let rightIndex;

 keypad.forEach((arr, index) => {
   if (arr.includes(number)) numIndex = [index, arr.indexOf(number)];
   if (arr.includes(left)) leftIndex = [index, arr.indexOf(left)];
   if (arr.includes(right)) rightIndex = [index, arr.indexOf(right)];
 });

 const leftDistance = checkDistance(numIndex, leftIndex);
 const rightDistance = checkDistance(numIndex, rightIndex);

 if (leftDistance === rightDistance) {
   if (hand === 'left') {
     hands.set('left', number);
     return 'L';
   } else {
     hands.set('right', number);
     return 'R';
   }
 } else {
   if (leftDistance < rightDistance) {
     hands.set('left', number);
     return 'L';
   } else {
     hands.set('right', number);
     return 'R';
   }
 }
}

function checkDistance(numIndex, hand) {
 if (numIndex[0] === hand[0]) {
   return Math.abs(numIndex[1] - hand[1]);
 }

 return Math.abs(numIndex[0] - hand[0]) + Math.abs(numIndex[1] - hand[1]);
}

🔶피드백
Set,Map 공부

0개의 댓글