이 문제는 매트릭스가 주어지고 이것이 시계 방향으로 90도 꺾인 것이 target에 맞는 것인지 알아내는 문제, 난이도는 easy 문제인데.. 이거 easy 맞냐..
문제가 주어졌을때 나는 Matrix 가 90도로 돌려졌을때 배치되는 요소들의 규칙을 찾았다.
이게 관건인거 같은데 사실 눈에 잘 안들어오는 것도 사실....
x0y0 => x2y0 (90도로 오른쪽 한번 돌려졌을때)
x0y0 => => x2y2 (90도로 오른쪽 두번 돌려졌을때)
근데 규칙은 눈에 안들어오더라... 그래서 풀이를 좀 봤다.
mat[i][j] !== target[j][width - 1 - i]
=> 90도로 한번 돌려졌을때
mat[i][j] !== target[height - 1 - i][width - 1 - j]
=> 90도로 두번 돌려졌을때
mat[i][j] !== target[height - 1 - j][i]
=> 90도로 세번 돌려졌을때
이거 알아낸 당신.. 천재야..
var findRotation = function(mat, target) {
let width = mat[0].length; //example 3 : 3
let height = mat.length; //example 3 : 3
let normal = true;
let rightOneTimes = true;
let rightTwoTimes = true;
let rightThreeTimes = true;
for(let i=0; i<height; i++) {
for(let j=0; j<width; j++) {
if(mat[i][j] !== target[i][j]) normal = false;
//아무것도 돌리지 않았을때 같지 않으면 우선 false;
if(mat[i][j] !== target[j][width-1-i]) {
rightOneTimes = false;
}
// 오른쪽으로 한번만 돌렸을때 같지않으면 false
if(mat[i][j] !== target[height-1-i][width-1-j]) {
rightTwoTimes = false;
}
//오른쪽으로 두번 돌렸을때 같지 않으면 false
if(mat[i][j] !== target[height-1-j][i]) {
rightThreeTimes = false;
}
//오른쪽으로 세번 돌렸을때 같지 않으면 false
}
}
if(!normal && !rightOneTimes && !rightTwoTimes && !rightThreeTimes) return false;
//셋중에 하나라도 false 이면 이건 그냥 false
return normal || rightOneTimes || rightTwoTimes || rightThreeTimes;
//근데 전부 true 이면 true 반환하기
};
그래서 탄생한 정답!