|
| 1 | +//https://github.com/codeisneverodd/programmers-coding-test |
| 2 | +//더 좋은 풀이가 존재할 수 있습니다. |
| 3 | +//정답 1 - 본인의 깃허브 아이디 |
| 4 | +function solution(arr) { |
| 5 | + const quad = matrix => { |
| 6 | + const length = matrix.length; |
| 7 | + const half = length / 2; |
| 8 | + const pass = matrix => matrix.every(row => row.every(v => v === matrix[0][0])); |
| 9 | + |
| 10 | + if (pass(matrix)) return [matrix[0][0]]; |
| 11 | + if (length <= 2) return matrix; |
| 12 | + |
| 13 | + const startPoints = [ |
| 14 | + [0, 0], |
| 15 | + [0, half], |
| 16 | + [half, 0], |
| 17 | + [half, half], |
| 18 | + ]; |
| 19 | + |
| 20 | + return startPoints.map(([r, c]) => quad(matrix.slice(r, r + half).map(row => row.slice(c, c + half)))); |
| 21 | + }; |
| 22 | + return quad(arr) |
| 23 | + .flat(Infinity) |
| 24 | + .reduce((a, c) => (c === 0 ? [a[0] + 1, a[1]] : [a[0], a[1] + 1]), [0, 0]); |
| 25 | +} |
0 commit comments