프로그래머스 - 크레인 인형뽑기 게임
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(int[][] board, int[] moves) {
List<Integer> resultList = new ArrayList<>();
int count = 0;
for (int i = 0; i < moves.length; i++) {
int move = moves[i] - 1;
for (int j = 0; j < board.length; j++) {
if (board[j][move] != 0) {
resultList.add(board[j][move]);
board[j][move] = 0;
break;
}
}
if (resultList.size() >= 2) {
int k = resultList.size() - 1;
if (resultList.get(k).equals(resultList.get(k - 1))) {
resultList.remove(k);
resultList.remove(k - 1);
count += 2;
}
}
}
return count;
}
}
- 인형을 집어 올리고 결과 값이 2개 이상이라면 이전 인형과 비교
- 비교했을 때 동일한 인형이라면 리스트에서 제거해주고, 결과를 2 증가 시킴
- ArrayList를 remove하면 원소의 위치가 자동으로 조정되기 때문에 맨 뒤에서부터 제거하는 것이 좋음!