https://programmers.co.kr/learn/courses/30/lessons/67256?language=python3
코딩테스트 연습 - 키패드 누르기
[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"
programmers.co.kr
풀이 과정
숫자 5를 중점으로 하는 2차원 좌표를 고려하고, 각 숫자에 dictionary를 통해 좌표를 할당하였다.
누른 숫자가 1, 4, 7이면 그곳으로 왼쪽 손가락의 위치를 옮겨주고 정답 문자열에 'L' 추가,
누른 숫자가 3, 6, 9이면 그곳으로 오른쪽 손가락의 위치를 옮겨주고 정답 문자열에 'R' 추가,
누른 숫자가 2, 5, 8, 0이면 숫자와 왼쪽, 오른쪽 손가락의 위치를 고려해서 더 가까운 손가락의 위치를 옮겨주고 L또는 R을 추가해주는 방식으로 문제를 해결하면 된다.
13 ~ 20번째 테스트 케이스에서 틀렸다고 나오는 경우가 있는데 거리를 구할 때, ceil()을 사용해서 실수를 올림해 정수로 거리가 출력되게 해주면 해결된다.
소스 코드
파이썬
import math
def distance(a, b):
return math.ceil(((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** (1/2))
def solution(numbers, hand):
answer = ''
left_hand_position = [-1, 2]
right_hand_position = [1, 2]
position_dict = {1 : [-1, -1], 2 : [0, -1], 3 : [1, -1], 4 : [-1, 0], 5 : [0, 0], 6 : [1, 0], 7 : [-1, 1], 8 : [0, 1], 9 : [1, 1], 0 : [0, 2]}
for number in numbers:
if str(number) in '147':
answer += 'L'
left_hand_position = position_dict[number]
if str(number) in '369':
answer += 'R'
right_hand_position = position_dict[number]
if str(number) in '2580':
if distance(position_dict[number], left_hand_position) == distance(position_dict[number], right_hand_position):
if hand == 'left':
answer += 'L'
left_hand_position = position_dict[number]
else:
answer += 'R'
right_hand_position = position_dict[number]
else:
if distance(position_dict[number], left_hand_position) < distance(position_dict[number], right_hand_position):
answer += 'L'
left_hand_position = position_dict[number]
else:
answer += 'R'
right_hand_position = position_dict[number]
return answer
자바스크립트
function distance(a, b) {
return Math.ceil(((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) ** (1/2))
}
function solution(numbers, hand) {
let answer = '';
const position_dict = {1: [-1, -1], 2: [0, -1], 3: [1, -1], 4: [-1, 0], 5: [0, 0], 6: [1, 0], 7: [-1, 1], 8: [0, 1], 9: [1, 1], 0: [0, 2]}
let left_hand_position = [-1, 2]
let right_hand_position = [1, 2]
for (number of numbers) { // 파이썬 생각하고 in 쓰면 안됨
if ('147'.indexOf(String(number)) !== -1) { // '147'안에 String(number)가 없으면 -1이 출력된다
answer += 'L'
left_hand_position = position_dict[number]
} else if ('369'.indexOf(String(number)) !== -1) {
answer += 'R'
right_hand_position = position_dict[number]
} else if ('2580'.indexOf(String(number)) !== -1) {
if (distance(left_hand_position, position_dict[number]) === distance(right_hand_position, position_dict[number])) {
if (hand === 'left') {
answer += 'L'
left_hand_position = position_dict[number]
} else {
answer += 'R'
right_hand_position = position_dict[number]
}
}
else if (distance(left_hand_position, position_dict[number]) < distance(right_hand_position, position_dict[number])) {
answer += 'L'
left_hand_position = position_dict[number]
}
else if (distance(left_hand_position, position_dict[number]) > distance(right_hand_position, position_dict[number])) {
answer += 'R'
right_hand_position = position_dict[number]
}
}
}
return answer;
}
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 음양 더하기 [파이썬] [자바스크립트] (0) | 2022.05.06 |
---|---|
프로그래머스 - 없는 숫자 더하기 [파이썬] [자바스크립트] (0) | 2022.05.06 |
프로그래머스 - 크레인 인형뽑기 게임 [파이썬] [자바스크립트] (0) | 2022.05.06 |
프로그래머스 - 숫자 문자열과 영단어 [파이썬] (0) | 2022.05.06 |
프로그래머스 - 신규 아이디 추천 [파이썬] (0) | 2022.05.05 |