https://school.programmers.co.kr/learn/courses/30/lessons/64061
function solution(board, moves) {
let height = board.length;
let basket = [];
let answer = 0;
moves.forEach((line, idx) => {
let flag = true;
for (let i=0; i<height; i++) {
if (board[i][line-1] !== 0 && flag) {
basket.push(board[i][line-1]);
board[i][line-1] = 0;
flag = false;
}
}
if (basket.length >= 2) {
if (basket.slice(-1)[0] === basket.slice(-2)[0]) {
basket.pop();
basket.pop();
answer += 2;
}
}
})
return answer;
}
const transpose = matrix =>
matrix.reduce(
(result, row) => row.map((_, i) => [...(result[i] || []), row[i]]),
[]
);
const solution = (board, moves) => {
const stacks = transpose(board).map(row =>
row.reverse().filter(el => el !== 0)
);
const basket = [];
let result = 0;
for (const move of moves) {
const pop = stacks[move - 1].pop();
if (!pop) continue;
if (pop === basket[basket.length - 1]) {
basket.pop();
result += 2;
continue;
}
basket.push(pop);
}
return result;
};
function solution(board, moves) {
var count =0;
var stack = [];
for(var i=0;i<moves.length;i++){
var now = moves[i]-1
for(var j=0;j<board.length;j++){
if(board[j][now]!=0){
if(stack[stack.length-1]===board[j][now]){
stack.pop();
count+=2;
}
else{
stack.push(board[j][now])
}
board[j][now] = 0;
break;
}
}
}
console.log(stack)
return count
}
function solution(board, moves) {
var answer = 0;
var arr = []
for(var i = 0; i < moves.length; i++){
for(var j = 0; j < board.length;j++){
if(board[j][moves[i]-1] !== 0){
arr.push(board[j][moves[i]-1])
board[j][moves[i]-1] = 0
break;
}
}
if (arr.length >=2){
if (arr[arr.length-1] === arr[arr.length-2]){
answer +=2
arr.splice(arr.length-2,2)
}
}
}
return answer;
}