ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/42894

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

[ํ’€์ด]

 

์ดˆ๊ธฐ ์„ธํŒ…

1. ๊ฐ ๋ธ”๋ก์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ง์‚ฌ๊ฐํ˜•์˜ ์ขŒํ‘œ์™€ ๋ธ”๋ก๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • block ๋ฐฐ์—ด์— [๋ธ”๋ก๋ฒˆํ˜ธ, x1, y1, x2, y2]ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ํ•ด๋‹น ๋ธ”๋ก์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
(x1, y1)   (x1, y2)
     
(x2, y1)   (x2, y2)
  • block ๋ฐฐ์—ด์˜ ์–ด๋Š index์— ๋ธ”๋ก๋ฒˆํ˜ธ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด Map์„ ํ™œ์šฉํ•ด (key = ๋ธ”๋ก๋ฒˆํ˜ธ, value = ํ•ด๋‹น ๋ธ”๋ก๋ฒˆํ˜ธ์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด block์˜ index) ํ˜•ํƒœ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

2. 1X1ํ˜•ํƒœ์˜ ๊ฒ€์€ ๋ธ”๋ก์„ ์ฑ„์›Œ์ฃผ๋Š” dropBlock ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

  • board๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฒ€์€ ๋ธ”๋ก์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์— -1์„ ๋„ฃ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋•Œ, ๊ฐ ์—ด๋งˆ๋‹ค 0~board.length-1๊นŒ์ง€ ๋ธ”๋ก์ด ์žˆ๋Š” ๊ณณ๊นŒ์ง€ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

 

ํ’€์ด ๊ณผ์ •

1. removeBlock ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ๋ธ”๋ก์„ ์ง€์›๋‹ˆ๋‹ค.

  • ์šฐ์„ , dropBlock ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ 1X1ํ˜•ํƒœ์˜ ๊ฒ€์€ ๋ธ”๋ก์„ ์ฑ„์›๋‹ˆ๋‹ค.
  • map์„ ํƒ์ƒ‰ํ•˜๋ฉฐ value(๋ธ”๋ก ์ •๋ณด์˜ index) ๋ฅผ ํ†ตํ•ด block ๋ฐฐ์—ด์—์„œ [idx, x1, y1, x2, y2] ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • ๋ธ”๋ก ์ •๋ณด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ง์‚ฌ๊ฐํ˜•์— 0์ด๋‚˜ ๋‹ค๋ฅธ ๋ธ”๋ก๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ง€์šธ ์ˆ˜ ์—†์œผ๋กœ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
  • ์ง€์šธ ์ˆ˜ ์žˆ๋Š” ๋ธ”๋ก์ด๋ผ๋ฉด isChange, answer, block์„ ๊ฐฑ์‹ ํ•˜๊ณ  map์—์„œ delete์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋ธ”๋ก๋ฒˆํ˜ธ๋ฅผ ์ง€์›๋‹ˆ๋‹ค. (๋ธ”๋ก์ด ์žˆ๋˜ ์œ„์น˜์˜ board๊ฐ’์„ 0์œผ๋กœ ๊ฐฑ์‹ )
  • isChange๊ฐ€ true๋ผ๋ฉด board๊ฐ€ ๊ฐฑ์‹ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— removeBlock ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœ ํ•ฉ๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ removeBlock ํ•จ์ˆ˜์˜ ์žฌ๊ท€ํ˜ธ์ถœ์ด ๋๋‚˜๋ฉด answer์— ์ง€์›Œ์ง„ ๋ธ”๋ก์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

function solution(board) {
    var answer = 0;
    
    // [idx, x1, y1, x2, y2];
    const block = [];
    const map = new Map();
    
    for(let i=0;i<board.length;i++) {
        for(let j=0;j<board.length;j++) {
            if(board[i][j] > 0 && !map.has(board[i][j])) {
                let x1, y1, x2, y2;
                let idx = board[i][j];
                x1 = i;
                x2 = i;
                y1 = Infinity;
                y2 = -Infinity;
                for(let k=i;k<board.length;k++) {
                    if(!board[k].includes(idx)) {
                        break;
                    }
                    x2 = k;
                    y1 = Math.min(y1, board[k].indexOf(idx));
                    y2 = Math.max(y2, board[k].lastIndexOf(idx));
                }
                map.set(idx, block.length);
                block.push([idx, x1, y1, x2, y2]);  
            }
        }
    }

    const dropBlock = () => {
        for(let i=0;i<board.length;i++) {
            for(let j=0;j<board.length;j++) {
                if(board[j][i] > 0) {
                    break;
                }
                board[j][i] = -1;
            }
        }
    }
    
    const removeBlock = () => {
        let isChange = false;
        if(map.size === 0) {
            return;
        }
        
        dropBlock();
        
        map.forEach((value, key) => {
            const [idx, x1, y1, x2, y2] = block[value];
            let rm = true;
            for(let i=x1;i<=x2;i++) {
                for(let j=y1;j<=y2;j++) {
                    if(board[i][j] !== idx && board[i][j] >= 0) {
                        rm = false;
                        break;
                    }
                }
            }
            // ์ง€์šธ ์ˆ˜ ์žˆ๋Š” ๋ธ”๋ก์ž„
            if(rm) {
                isChange = true;
                for(let i=x1;i<=x2;i++) {
                    for(let j=y1;j<=y2;j++) {
                        board[i][j] = 0;
                    }
                }
                map.delete(idx);
                answer++;
            }
        })
        
        if(isChange) {
            removeBlock();
        }
    }
    
    removeBlock();
    
    return answer;
}