|
| 1 | +/** |
| 2 | + * @param {number[][]} matrix |
| 3 | + * @return {number[][]} |
| 4 | + */ |
| 5 | +const matrixRankTransform = function (matrix) { |
| 6 | + const m = matrix.length |
| 7 | + const n = matrix[0].length |
| 8 | + const rowIndex = Array.from({ length: m }, () => Array(n).fill(0)) |
| 9 | + const colIndex = Array.from({ length: m }, () => Array(n).fill(0)) |
| 10 | + for (let i = 0; i < m; i++) { |
| 11 | + let row = [] |
| 12 | + for (let j = 0; j < n; j++) { |
| 13 | + row.push([matrix[i][j], j]) |
| 14 | + } |
| 15 | + |
| 16 | + row.sort((a, b) => a[0] - b[0]) |
| 17 | + for (let j = 0; j < n; j++) { |
| 18 | + rowIndex[i][j] = row[j][1] |
| 19 | + } |
| 20 | + } |
| 21 | + for (let i = 0; i < n; i++) { |
| 22 | + const col = [] |
| 23 | + for (let j = 0; j < m; j++) { |
| 24 | + col.push([matrix[j][i], j]) |
| 25 | + } |
| 26 | + col.sort((a, b) => a[0] - b[0]) |
| 27 | + for (let j = 0; j < m; j++) { |
| 28 | + colIndex[j][i] = col[j][1] |
| 29 | + } |
| 30 | + } |
| 31 | + const result = Array.from({ length: m }, () => Array(n).fill(0)) |
| 32 | + for (let i = 0; i < m; i++) { |
| 33 | + for (let j = 0; j < n; j++) { |
| 34 | + result[i][j] = 1 |
| 35 | + } |
| 36 | + } |
| 37 | + let changed = true |
| 38 | + while (changed) { |
| 39 | + changed = shakeRow(matrix, rowIndex, result) |
| 40 | + changed = shakeCol(matrix, colIndex, result) || changed |
| 41 | + } |
| 42 | + return result |
| 43 | +} |
| 44 | + |
| 45 | +function shakeCol(matrix, colIndex, result) { |
| 46 | + let changed = false |
| 47 | + for (let i = 0; i < matrix[0].length; i++) { |
| 48 | + for (let j = 1; j < matrix.length; j++) { |
| 49 | + if (matrix[colIndex[j][i]][i] == matrix[colIndex[j - 1][i]][i]) { |
| 50 | + if (result[colIndex[j][i]][i] != result[colIndex[j - 1][i]][i]) |
| 51 | + changed = true |
| 52 | + result[colIndex[j][i]][i] = Math.max( |
| 53 | + result[colIndex[j][i]][i], |
| 54 | + result[colIndex[j - 1][i]][i] |
| 55 | + ) |
| 56 | + result[colIndex[j - 1][i]][i] = Math.max( |
| 57 | + result[colIndex[j][i]][i], |
| 58 | + result[colIndex[j - 1][i]][i] |
| 59 | + ) |
| 60 | + } else { |
| 61 | + if (result[colIndex[j][i]][i] < result[colIndex[j - 1][i]][i] + 1) { |
| 62 | + changed = true |
| 63 | + result[colIndex[j][i]][i] = result[colIndex[j - 1][i]][i] + 1 |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return changed |
| 69 | +} |
| 70 | + |
| 71 | +function shakeRow(matrix, rowIndex, result) { |
| 72 | + let changed = false |
| 73 | + for (let i = 0; i < matrix.length; i++) { |
| 74 | + let rowInd = rowIndex[i] |
| 75 | + let resu = result[i] |
| 76 | + for (let j = 1; j < matrix[0].length; j++) { |
| 77 | + if (matrix[i][rowInd[j]] == matrix[i][rowInd[j - 1]]) { |
| 78 | + if (resu[rowInd[j]] != resu[rowInd[j - 1]]) changed = true |
| 79 | + resu[rowInd[j]] = Math.max(resu[rowInd[j - 1]], resu[rowInd[j]]) |
| 80 | + resu[rowInd[j - 1]] = Math.max(resu[rowInd[j - 1]], resu[rowInd[j]]) |
| 81 | + } else { |
| 82 | + if (resu[rowInd[j]] < resu[rowInd[j - 1]] + 1) { |
| 83 | + changed = true |
| 84 | + resu[rowInd[j]] = resu[rowInd[j - 1]] + 1 |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return changed |
| 90 | +} |
0 commit comments