- 문제
- 인형을 뽑아서 담는 바구니에 대해서 크기가 정해져 있지 않고 한쪽으로만 삽입이 일어나는 구조이다 보니 Stack을 사용하는 것이 좋다고 생각하였다.
- 매번 바구니에 삽입이 일어날때 이전의 인형과 일치하면 삽입하는 인형과 이전의 인형을 삭제하기 때문에 일치 비교를 위해 Stack.peek()을 사용하였다.
- 인형을 뽑기 위해서 moves 별로 -1을 연산하여 column 값으로 사용하면서 board의 row를 순회하였다.
package kakao.intern2019;
import java.util.*;
public class Q1 {
public static void main(String[] args) {
int[][] board = {{0, 0, 0, 0, 0}, {0, 0, 1, 0, 3}, {0, 2, 5, 0, 1}, {4, 2, 4, 4, 2}, {3, 5, 1, 3, 1}};
int[] moves = {1, 5, 3, 5, 1, 2, 1, 4};
System.out.println(solution(board, moves));
}
public static int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> stack = new Stack<>();
for (int col : moves) {
// 크레인의 위치 = board 의 col
for (int[] row : board) {
// board 의 row,col 을 통해 해당 col 에서 인형이 있는 곳까지 접근
int value = row[col - 1];
if (value != 0) {
System.out.println(value);
if (stack.isEmpty()) {
stack.push(value);
} else {
if (stack.peek() == value) {
// 같은 인형 삭제 -> push 후 2번 pop
stack.pop();
answer += 2;
} else {
stack.push(value);
}
}
// 인형 빼고 나서는 0으로 바꾸어줌
row[col - 1] = 0;
// 처음 발견된 인형만 뺌
break;
}
}
}
return answer;
}
}