본문 바로가기
Algorithm/Programmers

[프로그래머스] 크레인 인형뽑기 게임 자바

by YOONAYEON 2021. 11. 7.
문제

 

 

 

코딩테스트 연습 - 크레인 인형뽑기 게임

[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4

programmers.co.kr

 

 

문제풀이

 

 

소스코드

 

import java.util.Stack;

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