Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ (javascript) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ (javascript)

Lennon 2021. 10. 20. 00:20
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/64061?language=javascript 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„

[[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]] [1,5,3,5,1,2,1,4] 4

programmers.co.kr

 

function solution(board, moves) {
    let stack = [];
    let count = 0;
    for(let i = 0; i < moves.length; i++){
        for(let j = 0; j < board.length; j++){
            if(board[j][moves[i]-1] === 0) continue;
            else {
                if(stack[stack.length-1] === board[j][moves[i]-1]){
                    board[j][moves[i]-1] = 0;
                    count+=2;
                    stack.pop();
                    break;
                }
                stack.push(board[j][moves[i]-1]);
                board[j][moves[i]-1] = 0;
                break;
            }
        }
    }
    return count;
}
728x90
๋ฐ˜์‘ํ˜•
Comments