์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

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

์€์ง„ 2021. 6. 27. 01:21

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 64061] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„ (Javascript)
๋ฌธ์ œ ์„ค๋ช…
๊ฒŒ์ž„๊ฐœ๋ฐœ์ž์ธ "์ฃ ๋ฅด๋””"๋Š” ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ธฐ๊ณ„๋ฅผ ๋ชจ๋ฐ”์ผ ๊ฒŒ์ž„์œผ๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
"์ฃ ๋ฅด๋””"๋Š” ๊ฒŒ์ž„์˜ ์žฌ๋ฏธ๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ํ™”๋ฉด ๊ตฌ์„ฑ๊ณผ ๊ทœ์น™์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒŒ์ž„ ๋กœ์ง์— ๋ฐ˜์˜ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ฒŒ์ž„ ํ™”๋ฉด์€ "1 x 1" ํฌ๊ธฐ์˜ ์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ "N x N" ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐ ๊ฒฉ์ž์ด๋ฉฐ ์œ„์ชฝ์—๋Š” ํฌ๋ ˆ์ธ์ด ์žˆ๊ณ  ์˜ค๋ฅธ์ชฝ์—๋Š” ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (์œ„ ๊ทธ๋ฆผ์€ "5 x 5" ํฌ๊ธฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค). ๊ฐ ๊ฒฉ์ž ์นธ์—๋Š” ๋‹ค์–‘ํ•œ ์ธํ˜•์ด ๋“ค์–ด ์žˆ์œผ๋ฉฐ ์ธํ˜•์ด ์—†๋Š” ์นธ์€ ๋นˆ์นธ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์ธํ˜•์€ "1 x 1" ํฌ๊ธฐ์˜ ๊ฒฉ์ž ํ•œ ์นธ์„ ์ฐจ์ง€ํ•˜๋ฉฐ ๊ฒฉ์ž์˜ ๊ฐ€์žฅ ์•„๋ž˜ ์นธ๋ถ€ํ„ฐ ์ฐจ๊ณก์ฐจ๊ณก ์Œ“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒŒ์ž„ ์‚ฌ์šฉ์ž๋Š” ํฌ๋ ˆ์ธ์„ ์ขŒ์šฐ๋กœ ์›€์ง์—ฌ์„œ ๋ฉˆ์ถ˜ ์œ„์น˜์—์„œ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ธํ˜•์„ ์ง‘์–ด ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง‘์–ด ์˜ฌ๋ฆฐ ์ธํ˜•์€ ๋ฐ”๊ตฌ๋‹ˆ์— ์Œ“์ด๊ฒŒ ๋˜๋Š” ๋ฐ, ์ด๋•Œ ๋ฐ”๊ตฌ๋‹ˆ์˜ ๊ฐ€์žฅ ์•„๋ž˜ ์นธ๋ถ€ํ„ฐ ์ธํ˜•์ด ์ˆœ์„œ๋Œ€๋กœ ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ์€ [1๋ฒˆ, 5๋ฒˆ, 3๋ฒˆ] ์œ„์น˜์—์„œ ์ˆœ์„œ๋Œ€๋กœ ์ธํ˜•์„ ์ง‘์–ด ์˜ฌ๋ ค ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด์€ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๊ฐ™์€ ๋ชจ์–‘์˜ ์ธํ˜• ๋‘ ๊ฐœ๊ฐ€ ๋ฐ”๊ตฌ๋‹ˆ์— ์—ฐ์†ํ•ด์„œ ์Œ“์ด๊ฒŒ ๋˜๋ฉด ๋‘ ์ธํ˜•์€ ํ„ฐ๋œจ๋ ค์ง€๋ฉด์„œ ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ์‚ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์œ„ ์ƒํƒœ์—์„œ ์ด์–ด์„œ [5๋ฒˆ] ์œ„์น˜์—์„œ ์ธํ˜•์„ ์ง‘์–ด ๋ฐ”๊ตฌ๋‹ˆ์— ์Œ“์œผ๋ฉด ๊ฐ™์€ ๋ชจ์–‘ ์ธํ˜• ๋‘ ๊ฐœ๊ฐ€ ์—†์–ด์ง‘๋‹ˆ๋‹ค.

ํฌ๋ ˆ์ธ ์ž‘๋™ ์‹œ ์ธํ˜•์ด ์ง‘์–ด์ง€์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ์—†์œผ๋‚˜ ๋งŒ์•ฝ ์ธํ˜•์ด ์—†๋Š” ๊ณณ์—์„œ ํฌ๋ ˆ์ธ์„ ์ž‘๋™์‹œํ‚ค๋Š” ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๋Ÿฐ ์ผ๋„ ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ๋ฐ”๊ตฌ๋‹ˆ๋Š” ๋ชจ๋“  ์ธํ˜•์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. (๊ทธ๋ฆผ์—์„œ๋Š” ํ™”๋ฉดํ‘œ์‹œ ์ œ์•ฝ์œผ๋กœ 5์นธ๋งŒ์œผ๋กœ ํ‘œํ˜„ํ•˜์˜€์Œ)

๊ฒŒ์ž„ ํ™”๋ฉด์˜ ๊ฒฉ์ž์˜ ์ƒํƒœ๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด board์™€ ์ธํ˜•์„ ์ง‘๊ธฐ ์œ„ํ•ด ํฌ๋ ˆ์ธ์„ ์ž‘๋™์‹œํ‚จ ์œ„์น˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด moves๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ํฌ๋ ˆ์ธ์„ ๋ชจ๋‘ ์ž‘๋™์‹œํ‚จ ํ›„ ํ„ฐํŠธ๋ ค์ ธ ์‚ฌ๋ผ์ง„ ์ธํ˜•์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

[์ œํ•œ์‚ฌํ•ญ]
board ๋ฐฐ์—ด์€ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํฌ๊ธฐ๋Š” "5 x 5" ์ด์ƒ "30 x 30" ์ดํ•˜์ž…๋‹ˆ๋‹ค.
board์˜ ๊ฐ ์นธ์—๋Š” 0 ์ด์ƒ 100 ์ดํ•˜์ธ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค.
0์€ ๋นˆ ์นธ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
1 ~ 100์˜ ๊ฐ ์ˆซ์ž๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์ธํ˜•์˜ ๋ชจ์–‘์„ ์˜๋ฏธํ•˜๋ฉฐ ๊ฐ™์€ ์ˆซ์ž๋Š” ๊ฐ™์€ ๋ชจ์–‘์˜ ์ธํ˜•์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
moves ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 1 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
moves ๋ฐฐ์—ด ๊ฐ ์›์†Œ๋“ค์˜ ๊ฐ’์€ 1 ์ด์ƒ์ด๋ฉฐ board ๋ฐฐ์—ด์˜ ๊ฐ€๋กœ ํฌ๊ธฐ ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ
board moves result
[[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
์ž…์ถœ๋ ฅ ์˜ˆ์— ๋Œ€ํ•œ ์„ค๋ช…
์ž…์ถœ๋ ฅ ์˜ˆ #1

์ธํ˜•์˜ ์ฒ˜์Œ ์ƒํƒœ๋Š” ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํฌ๋ ˆ์ธ์ด [1, 5, 3, 5, 1, 2, 1, 4] ๋ฒˆ ์œ„์น˜์—์„œ ์ฐจ๋ก€๋Œ€๋กœ ์ธํ˜•์„ ์ง‘์–ด์„œ ๋ฐ”๊ตฌ๋‹ˆ์— ์˜ฎ๊ฒจ ๋‹ด์€ ํ›„, ์ƒํƒœ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์œผ๋ฉฐ ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๋Š” ๊ณผ์ •์—์„œ ํ„ฐํŠธ๋ ค์ ธ ์‚ฌ๋ผ์ง„ ์ธํ˜•์€ 4๊ฐœ ์ž…๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-26

1. ๊ฐ™์€ ์ธํ˜•์ด ์Šคํƒ์— ์Œ“์ด๋ฉด ํ„ฐ์ง --> cnt++

  • 4, 3, 1, 1, 3, 2, 4 ์ˆœ์œผ๋กœ ์Œ“์ด๊ณ , 1์ด ๋จผ์ € ํ„ฐ์ง„ ํ›„ 3์ด ํ„ฐ์ง
  • ์ด 4๊ฐœ์˜ ์ธํ˜•์ด ํ„ฐ์ง

2. moves๋งŒํผ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰

  • moves๋ฅผ ์ธ๋ฑ์Šค ๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด x = x-1 ์ถ”๊ฐ€
for (let x of moves) { 
    x = x-1;
}

3. board ๊ฐ’์ด ์กด์žฌํ•  ๋•Œ๊นŒ์ง€ ์ˆœํšŒ

  • board[i][x] === 0 ์ด๋ฉด i++;
  • board[i][x] !== 0 ์ด๋ฉด stack.push( board[i][x] );, 0์ด ๋จ
for (let i=0; i<board.length; i++) {
    if (board[i][x]) {
        stack.push(board[i][x]);
        board[i][x] = 0;
    }
}

์ฃผ์˜
board[i][x] ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ์ˆœํšŒ ์ค‘์ง€!!
break;


4. stack ๊ตฌํ˜„ 1

  • ๊ฐ™์€ ์ธํ˜•์ด ์Œ“์ด๋ฉด ํญ๋ฐœ, cnt + 2
  • stack.push( board[i][x] ); ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ํ™•์ธํ•˜๋ฉด ๋” ์ข‹์„ ๋“ฏ?
  • board ๊ฐ’์ด stack์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๊ณผ ๋™์ผํ•  ๊ฒฝ์šฐ, stack.pop(), cnt + 2
for (let i=0; i<board.length; i++) {
    if (board[i][x] === stack[stack.length-1]) {
        stack.pop();
        board[i][x] = 0;
        cnt += 2;
    }
    else if (board[i][x]) {
        stack.push(board[i][x]);
        board[i][x] = 0;
    }
}

5. stack ๊ตฌํ˜„ 2

  • board[i][x] ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด ๋ฌด์กฐ๊ฑด stack.push()
  • stack[stack.length-1]๊ณผ [stack[stack.length-2]๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธ


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-26 ์ •ํ™•์„ฑ ํ†ต๊ณผ, ํ…Œ์ผ€ 4๋ฒˆ 4.45ms, 33.1MB

  • ์Šคํƒ์— ๋„ฃ๊ธฐ ์ „, board ๊ฐ’๊ณผ stack ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ
function solution(board, moves) {
    let cnt = 0;
    let stack = [];

    for (let x of moves) {
        x = x - 1;

        for (let i = 0; i < board.length; i++) {
            if (board[i][x] === stack[stack.length - 1]) {
                stack.pop();
                board[i][x] = 0;
                cnt += 2;
                break;
            }
            else if (board[i][x]) {
                stack.push(board[i][x]);
                board[i][x] = 0;
                break;
            }
        }
    }

    return cnt;
}

2021-06-26 ์ •ํ™•์„ฑ ํ†ต๊ณผ, ํ…Œ์ผ€ 4๋ฒˆ 0.95ms, 30.1MB

  • board ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด ๋ฌด์กฐ๊ฑด stack์— push
  • push ํ•œ ํ›„ ๋‘ ์ธํ˜•์˜ ์ผ์น˜์—ฌ๋ถ€ ํŒ๋ณ„
function solution(board, moves) {
    let cnt = 0;
    let stack = [];

    for (let x of moves) {
        x = x - 1;

        for (let i = 0; i < board.length; i++) {
            if (board[i][x]) {
                stack.push(board[i][x]);
                board[i][x] = 0;

                let len = stack.length;
                if (stack[len-1] === stack[len-2]) {
                    stack.pop();
                    stack.pop();
                    cnt += 2;
                }
                break;
            }
        }
    }
    return cnt;
}


โœ๏ธ๋ช…๋‹ต

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;
};


๋Œ“๊ธ€ ์ธ์šฉ

๋‹ค์‹œ ์ฝ์–ด๋ณด๊ธฐ

์˜ˆ์‹œ๋ฅผ ๋“ค์–ด, let A = transpose([[1, 2, 3], [4, 5, 6]]) ๋ผ๊ณ  ํ•ด๋ณด์ฃ . transpose์—์„œ ๊ฐ ์š”์†Œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ๋‘ ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค. reduce์™€ map์ž…๋‹ˆ๋‹ค. reduce๋Š” [1,2,3]๊ณผ [4,5,6]์ด ์š”์†Œ๊ฐ€ ๋  ๊ฒƒ์ด๋ฉฐ, map์€ reduce๊ฐ€ [1,2,3]์ผ ๋•Œ 1๊ณผ 2์™€ 3์ด ๋˜๋ฉฐ, [4,5,6]์ผ ๋•Œ๋Š” 4,5,6์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ map์€ ๊ฐ ์š”์†Œ์— โ€˜ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’โ€™์œผ๋กœ ๋Œ€์ฒดํ•ด์ค€๋‹ค๊ณ  ํ–ˆ์ฃ ? ๊ทธ๋ ‡๋‹ค๋ฉด map((_, i) => [(result[i] || []), row[i]])์—์„œ reduce = [1,2,3]์ผ ๋•Œ 1 ์€ map = [โ€ฆ(result[0]||[]),row[0])์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ result[0]์€ [][0]์™€ ๊ฐ™๊ณ  ์ด ๊ฐ’์€ null์ž…๋‹ˆ๋‹ค. [โ€ฆ(null||[]),row[0])๊ณผ ๋˜‘๊ฐ™๊ธฐ์— ์ด ๊ฒฐ๊ณผ๋Š” [โ€ฆ[],1]์ด ๋ฉ๋‹ˆ๋‹ค. (์—ฌ๊ธฐ์„œ A||B๋Š” A์˜ ๊ฐ’์ด null์ด๋‚˜ undefined๊ฐ€ ๋ผ๋ฉด B๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด A๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.) ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ 2์™€ 3์€ [โ€ฆ[],2],[โ€ฆ[],3]์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ reduce = [1,2,3]์ผ ๋•Œ map์„ ํ†ตํ•ด์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์€ [[โ€ฆ[],1],[โ€ฆ[],2],[โ€ฆ[],3]]์ด ๋˜๋ฉฐ, โ€ฆ๋ฌธ๋ฒ•์€ ๋นˆ๋ฐฐ์—ด์„ ๋ฌด์‹œํ•ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— [[1],[2],[3]]์ด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ reduce๊ฐ€ [4,5,6]์ผ ๋•Œ์— result์—๋Š” โ€˜์ด์ „ ์ธ๋ฑ์Šค์˜ ๋ฐ˜ํ™˜๊ฐ’โ€™์ธ [[1],[2],[3]]๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ reduce๊ฐ€ [4,5,6]์ผ ๋•Œ์—๋Š” 4์—์„œ [โ€ฆ(result[0],[]),row[0])์ธ๋ฐ ์ด ๊ฐ’์€, [โ€ฆ([1]||[]),row[0])๊ณผ ๊ฐ™๊ณ  ์ด๋Š” [โ€ฆ[1],4]์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ! โ€ฆ๋ฌธ๋ฒ•์—์„œ๋Š” ๋ฐฐ์—ด ์•ˆ์˜ ์š”์†Œ๋ฅผ ๋„์ง‘์–ด ๋‚ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋Š” [1,4]์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ดํ›„์—๋Š” [2,5],[3,6]์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ A์—๋Š” [[1,2,3],[4,5,6]]์„ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ณ€ํ™˜ํ•ด์ค€ [[1,4],[2,5],[3,6]]์ด ์ €์žฅ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ฌธ์ œ์—์„œ๋Š” transpose([[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]])์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ๊ฐ’์€ ๊ทธ๋ ‡๋‹ค๋ฉด, [[0, 0, 0, 4, 3],[0, 0, 2, 2, 5],[0, 1, 5, 4, 1],[0, 0, 0, 4, 3],[0, 3, 1, 2, 1]]์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” 5X5๋ฐฐ์—ด์˜ ํ–‰๊ณผ ์—ด์„ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ง€์š”