[프로그래머스](python) 크레인 인형뽑기 게임 -2019 카카오 개발자 인턴십

berry ·2021년 4월 21일
0

Algorithm

목록 보기
2/77
post-thumbnail

문제

코린이인 나는
문제부터 좀 많이 읽고나서 이해했다...

다른 사람의 깔끔한 코드

def solution(board, moves):
    stacklist = []
    answer = 0

    for i in moves:
        for j in range(len(board)):
            if board[j][i-1] != 0:
                stacklist.append(board[j][i-1])
                board[j][i-1] = 0

                if len(stacklist) > 1:
                    if stacklist[-1] == stacklist[-2]:
                        stacklist.pop(-1)
                        stacklist.pop(-1)
                        answer += 2
                break

    return answer

moves에서 움직임을 하나씩 출력하고
board[j][i-1]이 0이 아닐 때(인형이 있을 때)
stacklist에 차례로 더한다.
그리고 board[j][i-1]는 텅 비었다.
(그만큼 인형뽑기를 잘하신다는 거지)

stacklist에 인형이 1개 이상 있으면
또 stacklist[-1]에 있는 인형과
stacklist[-2]에 있는 인형이 같다면
하나씩 빼낸다.
그리고 빼낸 인형을 answer에 더한다.

move를 처음 알고 이해하느라 약간 ^.~

profile
Engineer

0개의 댓글