최대한 가독성을 위해 함수를 분리하고 직관적인 변수명을 지으려고 노력했다.
function solution(board, moves) {
var popCount = 0;
let playBoard = board.slice();
let bucket = [];
for (let move of moves) {
let doll = getDoll(playBoard, move - 1);
let lastDoll = bucket[bucket.length - 1];
if (doll) {
if (doll === lastDoll) {
bucket.pop();
popCount++;
} else {
bucket.push(doll);
}
}
}
return popCount * 2;
}
function getDoll(board, col) {
let doll = 0;
for (let row of board) {
doll = row[col];
if (doll) {
row[col] = 0;
return doll;
}
}
return doll;
}
첫 번째 시도를 하고 나서 다른 블로그 글을 보니 신기한 방식으로 하는 분이 계셔서 코드를 보지는 않았고 컨셉만 가지고 직접 구현해보기로 했다. 어떻게 이런 생각을 하셨지.
(to be continue..)
참고 블로그 : HongdaeDev.log님의 블로그