항상 자료구조는 비었을 때, 비지 않았을 때를 체크해서 예외를 방지해야한다!!!!
public static int solution(int[][] board, int[] moves) {
int answer = 0;
int movesIdx;
Stack<Integer> temp = new Stack<>();
for (int move : moves) {
movesIdx = move - 1;
for (int j = 0; j < board.length; j++) { // 열 고정, 행만 내림
if (board[j][movesIdx] != 0) {
if (temp.isEmpty()) {
temp.push(board[j][movesIdx]);
} else {
if (temp.peek() == board[j][movesIdx]) {
answer += 2;
temp.pop();
} else {
temp.push(board[j][movesIdx]);
}
}
board[j][movesIdx] = 0;
break; // 안걸면 인형 계속 가져온다
}
}
}
return answer;
}