본문 바로가기

프로그래머스 알고리즘 문제/Level 1

프로그래머스 Level 1 - 크레인 인형 뽑기

내가 제출한 정답:

 

def solution(board, moves):
    answer = 0
    stack = []
    def checkStack(stck):
        length = len(stck)
        if length < 2:
            return False
        elif stck[length-1] == stck[length-2]:
            stck.pop()
            stck.pop()
            return True
        return False
    for move in moves:
        for row in board:
            if row[move-1]:
                stack.append(row[move-1])
                row[move-1] = 0
                if checkStack(stack): answer += 2
                break
    return answer

 

다른 사람의 정답들:

 

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