230727 크레인 인형뽑기 게임

Jongleee·2023년 7월 27일
0

TIL

목록 보기
322/576
public int solution(int[][] board, int[] moves) {
	int answer = 0;

	Stack<Integer> stack = new Stack<>();
	stack.push(0);

	for (int move : moves) {
		int columnIndex = move - 1;

		for (int row = 0; row < board.length; row++) {
			int currentValue = board[row][columnIndex];

			if (currentValue != 0) {
				if (stack.peek() == currentValue) {
					stack.pop();
					answer += 2;
				} else {
					stack.push(currentValue);
				}

				board[row][columnIndex] = 0;
				break;
			}
		}
	}

	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/64061

0개의 댓글