4 Aug 2020

2019 카카오 겨울 인턴 크레인 인형뽑기 게임 [Java]

2019 카카오 겨울 인턴 크레인 인형뽑기 게임 [Java]


import java.util.*;
class Solution {
    //인형 뽑기 기계에서 인형을 뽑는다.
    public int pick(int[][] board,int pos){
        int picked=-1;
        for(int i=0;i<board.length;i++){
            if(board[i][pos-1]!=0){
                picked=board[i][pos-1];
                board[i][pos-1]=0;
                break;
            }
        }
        return picked;
    }

    //바구니에서 인형이 터지는지 검사
    public boolean check(Stack<Integer> st){
        boolean result=false;
        if(st.size()>=2){
            int first=st.pop();
            int second=st.peek();
            if(first==second){
                st.pop();
                result=true;
            }else{
                st.push(first);
            }
            return result;
        }
        return result;
    }

    //바구니에 인형 넣기
    public boolean push(Stack<Integer> st,int doll){
        st.push(doll);
        return check(st);
    }
    
    public int solution(int[][] board, int[] moves) {
        int answer = 0;
        Stack<Integer> st=new Stack<>();
        for(int pos:moves){
            int picked=pick(board,pos);
            if(picked!=-1&&push(st,picked)){
                answer+=2;
            }
        }
        return answer;
    }
}


Tags:
0 comments