

풀이
재영
const getCopiedBoard = (board) => { return JSON.parse(JSON.stringify(board)); }; function reverseColumn(board, idx) { for (let i = 0; i < board.length; i += 1) { board[i][idx] = +!board[i][idx]; } } function deepEqual(target, board) { return JSON.stringify(target) === JSON.stringify(board); } function check(board, target, col) { let flag = false; for (let row = 0; row < board.length; row += 1) { if (board[row][col] !== target[row][col]) { flag = true; break; } } return flag; } const solution = (beginning, target) => { let result = Infinity; const rowLength = beginning.length; const colLength = beginning[0].length; // 1. 행의 모든 경우의 수를 구한다. // 2. 행의 경우에 따라서 업데이트 해야하는 열을 업데이트해준다. (타깃과 비교해서 다르면) // 3. JSON.stringify() 직렬화를 해서 간단하게 두 배열 비교 -> 같으면 result 업데이트 // 5 -> 2진수 10000 = 32 2^5 = 모든 경우의 수. 행의 모든 경우의 수 const rowCases = 1 << rowLength; for (let bit = 0; bit < rowCases; bit += 1) { let reverseCount = 0; const board = getCopiedBoard(beginning); for (let row = 0; row < rowLength; row += 1) { const nowRowBit = 1 << row; // 현재 행을 비트화해준 거에요. if (!(nowRowBit & bit)) { board[row] = board[row].map((v) => +!v); reverseCount += 1; } } for (let col = 0; col < colLength; col += 1) { const shouldReverse = check(board, target, col); if (shouldReverse) { reverseColumn(board, col); reverseCount += 1; } } if (deepEqual(target, board)) { result = Math.min(result, reverseCount); } } return result === Infinity ? -1 : result; };