Skip to content

Commit 0b721b6

Browse files
authored
Create 2946-matrix-similarity-after-cyclic-shifts.js
1 parent 4a2985f commit 0b721b6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @param {number} k
4+
* @return {boolean}
5+
*/
6+
var areSimilar = function(mat, k) {
7+
const m = mat.length, n = mat[0].length
8+
const clone = Array.from({ length: m }, () => Array(n).fill(null))
9+
for(let i = 0; i < m; i++) {
10+
for(let j = 0; j < n; j++) {
11+
clone[i][j] = mat[i][j]
12+
}
13+
}
14+
for(let i = 0; i < m; i++) {
15+
const odd = i % 2 === 1
16+
if(odd) {
17+
sr(clone, i)
18+
} else {
19+
sl(clone, i)
20+
}
21+
}
22+
// console.log(clone)
23+
for(let i = 0; i < m; i++) {
24+
for(let j = 0; j < n; j++) {
25+
if(mat[i][j] !== clone[i][j]) return false
26+
}
27+
}
28+
29+
30+
return true
31+
32+
function sr(mat, i) {
33+
const row = mat[i]
34+
const idx = k % n
35+
mat[i] = row.slice(n - idx).concat(row.slice(0, n - idx))
36+
}
37+
function sl(mat, i) {
38+
const row = mat[i]
39+
const idx = k % n
40+
mat[i] = row.slice(idx, n).concat(row.slice(0, idx))
41+
}
42+
};

0 commit comments

Comments
 (0)