코딩 테스트/프로그래머스 level1

크레인 인형뽑기 게임

fullfish 2022. 9. 8. 01:37

코드

function solution(board, moves) {
  let result = 0;
  let temp = [];
  for (let i = 0; i < moves.length; i++) {
    for (let j = 0; j < board.length; j++) {
      if (board[j][moves[i] - 1] !== 0) {
        temp.push(board[j][moves[i] - 1]);
        board[j][moves[i] - 1] = 0;
        if (temp[temp.length - 1] === temp[temp.length - 2]) {
          temp = temp.slice(0, temp.length - 2);
          result += 2;
        }
        break;
      }
    }
  }
  return result;
}