[레벨1] 크레인 게임 (JAVA)

Fekim·2022년 3월 15일
0

ps

목록 보기
43/48
  • 스택을 사용하는 문제
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){
            for(int j=0; j<board[0].length; ++j){
                
                if(board[j][moves[i]-1] != 0){
                    if(!stack.isEmpty() && stack.peek() == board[j][moves[i]-1]){
                        stack.pop();
                        answer += 2;
                    }
                    else{
                        stack.push(board[j][moves[i]-1]);
                    }
                    board[j][moves[i]-1] = 0;
                    break;
                }
            }
        }
        return answer;
    }
}
profile
★Bugless 2024★

0개의 댓글