inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 311. 크레인 인형뽑기 게임

    프로그래머스 (64061)
    Stupefyee's avatar
    Stupefyee
    Jun 11, 2025
    [JAVA 문제 풀이] 311. 크레인 인형뽑기 게임
    Contents
    내가 작성한 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/64061
    notion image
     

    내가 작성한 코드

    import java.util.*; class Solution { public int solution(int[][] board, int[] moves) { Stack<Integer> stack = new Stack<>(); int answer = 0; for (int move : moves) { int column = move - 1; // moves는 1부터 시작하므로 0으로 맞춤 for (int row = 0; row < board.length; row++) { if (board[row][column] != 0) { // 해당 위치에 인형이 있다면 int doll = board[row][column]; board[row][column] = 0; // 인형을 꺼내고 해당 위치를 비움 if (!stack.isEmpty() && stack.peek() == doll) { stack.pop(); // 같은 인형이 있으면 제거 answer += 2; // 제거된 인형의 개수 증가 } else { stack.push(doll); // 스택이 비어있거나 다른 인형이면 스택에 추가 } break; // 한 번 꺼낸 후 다음 move로 넘어감 } } } return answer; } }
    Share article
    Contents
    내가 작성한 코드

    stupefyee

    RSS·Powered by Inblog