프로그래머스 - 키패드 누르기 [JS]

2023. 1. 16. 12:39·알고리즘(Algorithm)
728x90
반응형

풀이

keypad를 배열로 만들면 반복문을 사용해야한다는 단점이 있기에 객체로 만들어서

각 손가락 위치에 해당하는 값들을 계산해 비교 -> finger

function solution(numbers, hand) {
  let res=[]
  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]
  }
  let currentLeftFinger='*'
  let currentRightFinger='#'
  const helper=(lf,rf,num)=>{
    const left=Math.abs(keypad[lf][0]-keypad[num][0])+Math.abs(keypad[lf][1]-keypad[num][1])
    const right=Math.abs(keypad[rf][0]-keypad[num][0])+Math.abs(keypad[rf][1]-keypad[num][1])
    if(left===right) return hand==="right" ? "R":"L"
    return left > right ? "R":"L"
  }
  for (let i = 0; i < numbers.length; i++) {
    if(numbers[i]===1||numbers[i]===4||numbers[i]===7){
      res.push("L")
      currentLeftFinger=numbers[i]
    }
    else if(numbers[i]===3||numbers[i]===6||numbers[i]===9){
      res.push("R")
      currentRightFinger=numbers[i]
    }
    else{
      let finger=helper(currentLeftFinger,currentRightFinger,numbers[i])
      if(finger==='L') currentLeftFinger=numbers[i]
      if(finger==='R') currentRightFinger=numbers[i]
      res.push(finger)
    }
  }
  return res.join('')
}

solution([1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5],"right")
728x90
반응형
저작자표시 (새창열림)

'알고리즘(Algorithm)' 카테고리의 다른 글

프로그래머스 - 숫자의 표현 [JS]  (0) 2023.01.30
프로그래머스 - 명예의 전당  (1) 2023.01.24
프로그래머스 - 크레인 인형뽑기 게임 [JS]  (0) 2023.01.16
프로그래머스 - 체육복(Greedy) [JS]  (1) 2023.01.13
프로그래머스 - 다트게임 [JS]  (0) 2023.01.11
'알고리즘(Algorithm)' 카테고리의 다른 글
  • 프로그래머스 - 숫자의 표현 [JS]
  • 프로그래머스 - 명예의 전당
  • 프로그래머스 - 크레인 인형뽑기 게임 [JS]
  • 프로그래머스 - 체육복(Greedy) [JS]
Hun-bot
Hun-bot
IT를 중심으로 다양한 것
  • Hun-bot
    로봇이 만드는 눈사람
    Hun-bot
  • 전체
    오늘
    어제
    • All Article (128)
      • Programmers (6)
        • TIP (1)
        • SQL (2)
        • LV1 (1)
        • LV2 (2)
        • LV3 (0)
      • Baekjoon (31)
        • Bronze (10)
        • Silver (19)
        • Gold (2)
        • Platinum (0)
        • Diamond (0)
      • Leetcode (0)
        • Easy (0)
        • Medium (0)
        • Hard (0)
        • SQL (0)
      • 알고리즘(Algorithm) (42)
      • JavaScript (40)
      • Linux (7)
      • JSP (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      Vue #Vue.js #정리
      Algorithm
      JSP #Vscode #톰켓 #Tomcat #Java #Web #jdk
      LeetCode #JS #Javascript #Algorithm
      파이썬 #입력 #python #input
      오블완
      알고리즘 #Algorithm
      JS #정규표현식
      JS #JavaScript #프로그래머스 #카카오
      알고리즘
      JS #클래스
      JS #javascript #객체 #Object
      자바스크립트 #연습문제
      리눅스 #입문
      자바스크립트
      Python #알고리즘
      JS #JavaScript #프로그래머스 #알고리즘
      c++
      고득점 Kit
      Javascript
      BaekJoon
      JavaScript #Set #Collection
      Programmers
      JS #프로그래머스 #숫자의표현 #알고리즘
      리눅스
      async await #js #문법 #자바스크립트 #비동기
      프로그래머스 #자바스크립트 #JS
      SQL
      프로그래머스
      티스토리챌린지
    • 최근 댓글

    • hELLO· Designed By정상우.v4.10.3
    Hun-bot
    프로그래머스 - 키패드 누르기 [JS]
    상단으로

    티스토리툴바

    티스토리툴바