public int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
stack.push(0);
for (int move : moves) {
int columnIndex = move - 1;
for (int row = 0; row < board.length; row++) {
int currentValue = board[row][columnIndex];
if (currentValue != 0) {
if (stack.peek() == currentValue) {
stack.pop();
answer += 2;
} else {
stack.push(currentValue);
}
board[row][columnIndex] = 0;
break;
}
}
}
return answer;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/64061