public void init(int[][] board, List<Stack<Integer>> l) {
for(int i = 0; i < board.length; i++) {
Stack<Integer> s = new Stack<>();
for(int j = board[i].length - 1; j >= 0; j--) {
int current = board[j][i];
if(current == 0) {
break;
}
s.push(current);
}
l.add(s);
}
}
public int pullDolls(List<Stack<Integer>> l, int[] moves) {
int answer = 0; // 터진 인형 개수
Stack<Integer> basket = new Stack<>();
for(int i = 0; i < moves.length; i++) {
int current = moves[i];
Stack s = l.get(current - 1);
if(!s.isEmpty()) {
int top = (int) s.pop();
if(!basket.isEmpty() && basket.peek() == top) {
basket.pop();
answer += 2;
} else {
basket.push(top);
}
}
}
return answer;
}
public int solution(int[][] board, int[] moves) {
List<Stack<Integer>> l = new ArrayList<>();
init(board, l); // 인형들 스택에 쌓아놓기
return pullDolls(l, moves); // 인형 뽑기
}