drexqq

[Java, 자바]Programmers - 크레인인형뽑기게임 본문

공부노트/Programmers

[Java, 자바]Programmers - 크레인인형뽑기게임

drexqq 2020. 6. 22. 12:41
728x90
반응형
class Solution {
    public int solution(int[][] board, int[] moves) {
        int answer = 0;
        Stack<Integer> basket = new Stack<Integer>();
        for (int i = 0; i < moves.length; i++) {
			for (int j = 0; j < board.length; j++) {
				if (board[j][moves[i]-1] != 0) {
					if (basket.isEmpty()) {
						basket.push(board[j][moves[i]-1]);
					}
					
					else {
						if (basket.peek() == board[j][moves[i]-1]) {
							basket.pop();
							answer += 2;
						}
						else {
							basket.push(board[j][moves[i]-1]);
						}
					}
					
					board[j][moves[i]-1] = 0;
					break;
				}
			}
		}
        System.out.println(answer);
        return answer;
    }
}

Stack을 이용하여 풀어봤다.

728x90
반응형
Comments