[프로그래머스] 크레인 인형뽑기 게임

코린이·2022년 6월 13일
0

프로그래머스

목록 보기
15/22

📢 "크레인 인형뽑기 게임" 문제

프로그래머스 문제 링크

🔎 풀이

사용언어 :python
answer는 뽑은 인형을 담는 list로
board[height][i-1]의 값이 0이 아닌 경우 answer에 그 값을 담고, 인형을 뽑았으니
board[height][i-1] = 0을 해준다.
이렇게 뽑을때 마다
answer의 마지막 두 값이 연속으로 같은지 확인해준다.
같으면 answer에서 삭제해주고 count+2를 해준다.

🔎 코드

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

    for i in moves:
        height = 0
        while height <= (len(board)-1):
            if board[height][i-1] == 0:
                height += 1
            else:
                answer.append(board[height][i-1])
                board[height][i-1] = 0
                break
        if len(answer) > 1:
            if answer[-1] == answer[-2]:
                count += 2
                del answer[-2:]

    return count
profile
초보 개발자

0개의 댓글