프로그래머스 1단계 "크레인 인형뽑기 게임"

sanha_OvO·2021년 6월 10일
0

Algorithm

목록 보기
45/84

문제

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


풀이

moves값에 해당하는 board의 열을 확인하여 0이 아닌 값이 나오면 스택에 그 값을 넣어주고 board의 해당자리에 0을 다시 넣어준다.
moves 값의 반복이 끝날 때 마다 스택 최상단의 두 수가 같은지 확인하여 answer값을 추가해 주면 된다.


Python 코드

def solution(board, moves):
    n = len(board)
    b_list = board
    stack = []
    answer = 0

    for a in moves:
      for i in range(n):
        if b_list[i][a-1] != 0:
          stack.append(b_list[i][a-1])
          b_list[i][a-1] = 0
          break
      
      try:
        if stack[-1] == stack[-2]:
          stack.pop()
          stack.pop()
          answer += 2
      except:
        continue
      
    return answer
profile
Web Developer / Composer

0개의 댓글