Problem Solving/Programmers

[Level 1] 크레인 인형뽑기 게임

kmkunk 2022. 3. 23. 15:17
import java.util.*;

class Solution {
    public int solution(int[][] board, int[] moves) {
        int answer = 0;
        Stack<Integer> stack = new Stack<>();
        
        for(int i=0; i<moves.length; i++) {
            int index = moves[i]-1;
            for(int j=0; j<board.length; j++) {
                if(board[j][index]==0) { continue; }
                else {
                    if(!stack.isEmpty() && stack.peek()==board[j][index]) {
                        stack.pop();
                        answer += 2;
                    } else {
                        stack.push(board[j][index]);
                    }
                    
                    board[j][index] = 0;
                    break;
                }
            }
        }
        
        return answer;
    }
}