my solution
public int solution(int[][] board, int[] moves) {
Stack<Integer> basket = new Stack<>();
int maxIndex = board.length - 1;
int popped = 0;
for (int move : moves) {
int colIndex = move - 1;
int rowIndex = 0;
while (rowIndex <= maxIndex && board[rowIndex][colIndex] == 0) {
rowIndex++;
}
if (rowIndex > maxIndex) {
continue;
}
int val = board[rowIndex][colIndex];
board[rowIndex][colIndex] = 0;
if (!basket.isEmpty() && basket.peek() == val){
basket.pop();
popped += 2;
} else {
basket.push(val);
}
}
return popped;
}
time complexity
O(M * N)
M = The number of moves (length of moves array)
N = The depth of the board (number of rows)