프로그래머스 lv1 크레인인형뽑기게임, 키패드누르기
1. 개요
- https://school.programmers.co.kr/learn/courses/30/lessons/64061
- https://school.programmers.co.kr/learn/courses/30/lessons/67256
2. 크레인인형뽑기게임 풀이
def solution(board, moves):
# board를 stack형태로 변경한다.
board = [list(reversed(row)) for row in zip(*board)]
stack = []
answer = 0
for move in [m-1 for m in moves]:
while board[move] and board[move][-1] == 0:
board[move].pop()
if not board[move]:
continue
new = board[move].pop()
if stack and stack[-1] == new:
stack.pop()
answer += 2
else:
stack.append(new)
return answer
3. 키패드누르기 풀이
# 00 01 02
# 10 11 12
# 20 21 22
# 30 31 32
def solution(numbers, hand):
R, C = [3,0,0,0,1,1,1,2,2,2], [1,0,1,2,0,1,2,0,1,2]
lr, lc, rr, rc = 3, 0, 3, 2
left, right = {1, 4, 7}, {3, 6, 9}
answer = []
for n in numbers:
if n in left:
lr, lc = R[n], C[n]
answer.append("L")
elif n in right:
rr, rc = R[n], C[n]
answer.append("R")
elif abs(lr-R[n])+abs(lc-C[n]) > abs(rr-R[n])+abs(rc-C[n]):
rr, rc = R[n], C[n]
answer.append("R")
elif abs(lr-R[n])+abs(lc-C[n]) < abs(rr-R[n])+abs(rc-C[n]):
lr, lc = R[n], C[n]
answer.append("L")
elif hand == "right":
rr, rc = R[n], C[n]
answer.append("R")
else:
lr, lc = R[n], C[n]
answer.append("L")
return "".join(answer)
Leave a comment