크레인 인형뽑기 게임

eunseo·2021년 7월 7일
0

Programmers

목록 보기
6/14



JAVA 문제풀이

import java.util.*;


class 크레인인형뽑기게임 {
    public int solution(int[][] board, int[] moves) {
        int answer = 0;
        
        Stack<Integer> stack = new Stack<>();
        
        for(int move : moves){
            for(int i=0; i< board.length; i++){
                if(board[i][move-1]!=0){
                    
                    if(stack.size()>=1 && stack.peek()== board[i][move-1]){
                        stack.pop();
                        answer+=2;
                    }else{
                      stack.push(board[i][move-1]);  
                    }
            
                    board[i][move-1] =0; 
                    break;
                }
            }
        }
        return answer;
    }
    
}

Python 문제풀이

def solution(board, moves):
    stack =[]
    answer =0

    for i in moves:
        for j in range(len(board)):
            if board[j][i-1] != 0:
               stack.append(board[j][i-1])
               board[j][i-1] =0


               if len(stack) >= 2 :
                    if stack[-1] == stack[-2]:
                        stack.pop()
                        stack.pop()
                        answer +=2

               
               break

    return answer
profile
backend developer

0개의 댓글

Powered by GraphCDN, the GraphQL CDN