반응형

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

 

 

 

 

 

 

참고: https://jokerldg.github.io/algorithm/2021/03/28/crane-doll.html

 

프로그래머스 크레인 인형뽑기 게임 (python 파이썬) - Tech

[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4

jokerldg.github.io

 

 

 

** Python

 

from collections import deque
def solution(board, moves):
    stacklist = deque()
    answer = 0
    for move in moves:
        for i in range(len(board)):
            if board[i][move-1] != 0:
                stacklist.appendleft(board[i][move-1])
                board[i][move-1] = 0
                
                if len(stacklist) > 1:
                    if stacklist[0] == stacklist[1]:
                        stacklist.popleft()
                        stacklist.popleft()
                        answer += 2
                break
    return answer

 

 

** Javascript

 

function solution(board, moves) {
    let answer = 0;
    let stackList = [];
    for (let move of moves) {
        for(let i = 0; i < board.length; i++) {
            if (board[i][move-1] !== 0) {
                stackList.unshift(board[i][move-1]);
                board[i][move-1] = 0;
                if (stackList.length > 0) {
                    if (stackList[0] === stackList[1]) {
                        stackList.shift();
                        stackList.shift();
                        answer += 2;
                    }
                }
                break;
            }
        }
    }
    return answer;
}
반응형

+ Recent posts