문제: https://school.programmers.co.kr/learn/courses/30/lessons/64061
import java.util.*;
class Solution {
public int solution(int[][] board, int[] moves) {
int len = board.length;
List<Stack<Integer>> list = new ArrayList<>(); // 0부터 시작
Stack<Integer> store = new Stack<>();
int answer = 0;
for(int i = 0; i < len; i++){
list.add(new Stack<>());
}
// 각 스택에 인형 넣기
for(int i = len -1 ; i > -1; i--){
for(int j = 0; j < len; j++){
if(board[i][j] != 0) list.get(j).push(board[i][j]);
}
}
// moves 돌며 하나씩 answerStack peek와 비교하기
for(int i : moves){
int curIdx = i-1;
if(list.get(curIdx).size() == 0) continue;
int curItem = list.get(curIdx).pop();
if(store.isEmpty()) store.push(curItem);
else{
int peekItem = store.peek();
if(peekItem == curItem){
answer += 2;
store.pop();
}else{
store.push(curItem);
}
}
}
return answer;
}
}