https://school.programmers.co.kr/learn/courses/30/lessons/64061
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]] 이런식이다.
Stack을 사용하여 peek함수로 제일 위의 값을 꺼내 크레인으로 뽑은 값과 같으면 pop()으로 제거하고 answer에 2를 더하고 원래 인형 위치의 값는 0으로 초기화 해준다.
import java.util.Stack;
class Solution {
public int solution(int[][] board, int[] moves) {
Stack<Integer> stack = new Stack<>();
int answer = 0;
for(int i = 0; i < moves.length; i++) {
int index = moves[i] - 1;
for(int j = 0; j < board.length; j++) {
// 인형이 있다면
if(board[j][index] > 0) {
if(!stack.isEmpty()){
//중복 값이 있다면
if(stack.peek() == board[j][index]){
stack.pop();
answer += 2;
board[j][index] = 0;
break;
}else{ //중복값이 없다면
stack.push(board[j][index]);
board[j][index] = 0;
break;
}
}else{ // 처음 바구니에 넣기
stack.push(board[j][index]);
board[j][index] = 0;
break;
}
}
}
}
return answer;
}
}