
😎풀이
k만큼 mat을 shift할 헬퍼 함수 정의
1-1. 짝수 인덱스 행이라면, 좌측 shift 연산 수행
1-2. 홀수 인덱스 행이라면, 우측 shift 연산 수행
- 두 배열의 모든 요소가 동일한지 판별할 헬퍼 함수 정의
mat를 k만큼 shift한 결과 shifted 정의
mat와 shifted 동등비교 결과 반환
function areSimilar(mat: number[][], k: number): boolean {
const shifted = shift(mat, k)
return isSimilar(mat, shifted)
};
function isSimilar(target1: number[][], target2: number[][]) {
const n = target1.length
const m = target1[0].length
for(let row = 0; row < n; row++) {
for(let col = 0; col < m; col++) {
if(target1[row][col] !== target2[row][col]) return false
}
}
return true
}
function shift(target: number[][], k:number) {
const clone = structuredClone(target)
const n = target.length
for(let row = 0; row < n; row++) {
const isEven = row % 2 === 0
for(let i = 0; i < k; i++) {
if(isEven) {
clone[row].push(clone[row].shift())
} else {
clone[row].unshift(clone[row].pop())
}
}
}
return clone
}