코딩테스트 연습 - 크레인 인형뽑기 게임 | 프로그래머스 (programmers.co.kr)
import java.util.Stack;
import java.util.Arrays;
class Solution {
public int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> result = new Stack<>();
int temp = 0;
for(int i : moves) {
for(int k = 0 ; k < board.length ; k++ ) {
int[] a = board[k];
if(a[i-1] == 0) continue;
if(!result.empty() && a[i-1] == result.peek() ) {
result.pop();
answer+=2;
}
else result.push(a[i-1]);
board[k][i-1] = 0;
break;
}
}
/*
for(int i= 0 ; i < board.length ; i++)
System.out.println(Arrays.toString(board[i] ));
System.out.println();
System.out.println();
System.out.println(result.toString());
*/
return answer;
}
}