Level1 - 크레인 인형뽑기 게임

손대중·2022년 3월 15일

문제 설명 및 링크

https://programmers.co.kr/learn/courses/30/lessons/64061?language=javascript

나의 풀이

  • 걍 moves 돌면서 체크하면 됨.
  • 다만 깔끔한 체크를 위해 board 를 행 기준이 아닌 열 기준으로 바꾼 이후에 moves 를 돔.

코드

모든 프로그래머스 문제 관련 코드들은 GitHub 링크 에 있음.

function solution(board, moves) {
    let count = 0;
    
    const reBoard = [];
    
    // board 재배치 (행 기준이 아닌 열 기준으로)
    board.map(arrRow => {
        arrRow.map((row, index) => {
            if (!reBoard[index]) {
                reBoard[index] = [];
            }
            
            if (row) {
                reBoard[index].push(row);
            }
        });
    });
    
    const greps = [];
    
    moves.map(m => {
        if (reBoard[m - 1].length === 0) {
            return;
        }
        
        const target = reBoard[m - 1].shift();
        
        if (greps.length > 0 && greps[greps.length - 1] === target) {
            greps.pop();
            count += 2;
        } else {
            greps.push(target);
        }
    });
    
    return count;
}

0개의 댓글