728x90
https://programmers.co.kr/learn/courses/30/lessons/67256
def solution(numbers, hand):
answer = ''
ans_arr = []
dist_2 = [1,0,1,2,1,2,3,2,3,4,3,4]
dist_5 = [2,1,2,1,0,1,2,1,2,3,2,3]
dist_8 = [3,2,3,2,1,2,1,0,1,2,1,2]
dist_0 = [4,3,4,3,2,3,2,1,2,1,0,1]
left_now = 10
right_now = 12
for i in numbers:
if i in [1,4,7]:
ans_arr.append('L')
left_now = i
elif i in [3,6,9]:
ans_arr.append('R')
right_now = i
elif i==2:
if dist_2[left_now-1] < dist_2[right_now-1] or (dist_2[left_now-1]==dist_2[right_now-1] and hand=='left'):
ans_arr.append('L')
left_now = i
if dist_2[left_now-1]>dist_2[right_now-1] or (dist_2[left_now-1]==dist_2[right_now-1] and hand=='right'):
ans_arr.append('R')
right_now = i
elif i==5:
if dist_5[left_now-1] < dist_5[right_now-1] or (dist_5[left_now-1]==dist_5[right_now-1] and hand=='left'):
ans_arr.append('L')
left_now = i
if dist_5[left_now-1]>dist_5[right_now-1] or (dist_5[left_now-1]==dist_5[right_now-1] and hand=='right'):
ans_arr.append('R')
right_now = i
elif i==8:
if dist_8[left_now-1] < dist_8[right_now-1] or (dist_8[left_now-1]==dist_8[right_now-1] and hand=='left'):
ans_arr.append('L')
left_now = i
if dist_8[left_now-1]>dist_8[right_now-1] or (dist_8[left_now-1]==dist_8[right_now-1] and hand=='right'):
ans_arr.append('R')
right_now = i
elif i==0:
if dist_0[left_now-1] < dist_0[right_now-1] or (dist_0[left_now-1]==dist_0[right_now-1] and hand=='left'):
ans_arr.append('L')
left_now = 11
if dist_0[left_now-1]>dist_0[right_now-1] or (dist_0[left_now-1]==dist_0[right_now-1] and hand=='right'):
ans_arr.append('R')
right_now = 11
answer = ''.join(ans_arr)
return answer
print(solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0],"right"))
이게 맞나...돌아가긴함
공간복잡도 조건 없길래 일단 이렇게 했는데 다른문제에서 조건 생기면 좀더 생각해봐야겠다...
또 소소한 팁

그렇다고 한다...문자열 다룰 때는 리스트로 처리하고 마지막에 문자열로 변환하자!
728x90
'프로그래머스' 카테고리의 다른 글
오픈채팅방 (0) | 2022.07.01 |
---|---|
문자열 압축 (0) | 2022.07.01 |
숫자 문자열과 영단어 (0) | 2022.06.23 |
신규 아이디 추천 (0) | 2022.06.22 |
로또의 최고 순위와 최저 순위 (0) | 2022.06.22 |