https://programmers.co.kr/learn/courses/30/lessons/64061?language=javascript#
그냥 stack을 사용해서 하면 된다.
function solution(board, moves) {
var answer = 0;
var stack = new Array();
for(let i = 0; i < moves.length ;i++){
let loc = moves[i];
for(let j = 0; j < board.length; j++){
if(0 < board[j][loc-1]){
let lastValue = board[j][loc-1];
if(stack[stack.length-1] == lastValue) {
answer+=2;
stack.pop();
}
else stack.push(board[j][loc-1]);
board[j][loc-1] = 0;
break;
}
}
}
return answer;
}
728x90
반응형
'Problem Solving' 카테고리의 다른 글
모의고사 (0) | 2020.09.02 |
---|---|
완주하지 못한 선수 (0) | 2020.09.02 |
1567. Maximum Length of Subarray With Positive Product (0) | 2020.09.01 |
1568. Minimum Number of Days to Disconnect Island (0) | 2020.09.01 |
Convert Sorted Array to Binary Search Tree (0) | 2020.08.27 |