https://programmers.co.kr/learn/courses/30/lessons/64061?language=javascript
모든 프로그래머스 문제 관련 코드들은 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;
}