개발의변화
프로그래머스 LV2 프렌즈블록 본문
반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/17679
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해설
function solution(m, n, board) {
let curBoard = [...board.map(block => [...block])].concat();
let newBoard = [...board.map(block => [...block])].concat();
console.log(curBoard,newBoard)
while (true) {
let count = 0;
for (let x = 0; x < m - 1; x++) {
for (let y = 0; y < n - 1; y++) {
if (curBoard[x][y] === '') continue;
if (curBoard[x][y] === curBoard[x][y + 1] && curBoard[x][y] === curBoard[x + 1][y] && curBoard[x][y] === curBoard[x + 1][y + 1]) {
newBoard[x][y] = '';
newBoard[x + 1][y] = '';
newBoard[x][y + 1] = '';
newBoard[x + 1][y + 1] = '';
count += 1;
}
}
}
for (let x = 0; x < m - 1; x++) {
for (let y = 0; y < n; y++) {
if (newBoard[x + 1][y] === '') {
for (let i = x; i >= 0; i--) {
newBoard[i + 1][y] = newBoard[i][y];
newBoard[i][y] = '';
}
}
}
}
curBoard = [...newBoard.map(block => [...block])].concat();
if (count === 0) break;
}
const allBoards = newBoard.reduce((acc, cur) => acc.concat(cur));
return allBoards.filter(block => block === '').length;
}
이차원배열을 두 개를 만들어서 curBoard에서 블록을 지울 것을 찾으면 그것을 newBoard에 지울 블록들을 넣는 방식을 활용해서 나타낸다
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 LV2 2 x n 타일링 (0) | 2023.04.13 |
---|---|
프로그래머스 LV2 질문목록 (0) | 2023.04.12 |
프로그래머스 LV2 파일명 정렬(카카오) (0) | 2023.04.06 |
프로그래머스 LV2 피보나치 수 (0) | 2023.04.02 |
프로그래머스 LV2 괄호 회전하기 (0) | 2023.04.01 |