|
| 1 | +/* |
| 2 | +
|
| 3 | +-* Toeplitz Matrix *- |
| 4 | +
|
| 5 | +Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false. |
| 6 | +
|
| 7 | +A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements. |
| 8 | +
|
| 9 | +
|
| 10 | +
|
| 11 | +Example 1: |
| 12 | +
|
| 13 | +
|
| 14 | +Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] |
| 15 | +Output: true |
| 16 | +Explanation: |
| 17 | +In the above grid, the diagonals are: |
| 18 | +"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]". |
| 19 | +In each diagonal all elements are the same, so the answer is True. |
| 20 | +Example 2: |
| 21 | +
|
| 22 | +
|
| 23 | +Input: matrix = [[1,2],[2,2]] |
| 24 | +Output: false |
| 25 | +Explanation: |
| 26 | +The diagonal "[1, 2]" has different elements. |
| 27 | +
|
| 28 | +
|
| 29 | +Constraints: |
| 30 | +
|
| 31 | +m == matrix.length |
| 32 | +n == matrix[i].length |
| 33 | +1 <= m, n <= 20 |
| 34 | +0 <= matrix[i][j] <= 99 |
| 35 | +
|
| 36 | +
|
| 37 | +Follow up: |
| 38 | +
|
| 39 | +What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once? |
| 40 | +What if the matrix is so large that you can only load up a partial row into the memory at once? |
| 41 | +
|
| 42 | +
|
| 43 | +*/ |
| 44 | + |
| 45 | +import 'dart:collection'; |
| 46 | + |
| 47 | +class A { |
| 48 | + bool isToeplitzMatrix(List<List<int>> matrix) { |
| 49 | + for (int i = 0; i < matrix.length - 1; i++) { |
| 50 | + for (int j = 0; j < matrix[0].length - 1; j++) { |
| 51 | + int ele = matrix[i][j]; |
| 52 | + if (i < matrix.length - 1 && |
| 53 | + j < matrix[0].length - 1 && |
| 54 | + matrix[i + 1][j + 1] != ele) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Solution when Only Load Data One Row at a Time |
| 64 | +class B { |
| 65 | + bool isToeplitzMatrix(List<List<int>> matrix) { |
| 66 | + if (matrix.length <= 1 || matrix[0].length <= 1) return true; |
| 67 | + Queue<int> q = Queue(); |
| 68 | + for (int i = matrix[0].length - 1; i >= 0; i--) { |
| 69 | + //set criteria |
| 70 | + q.add(matrix[0][i]); |
| 71 | + } |
| 72 | + for (int j = 1; j < matrix.length; j++) { |
| 73 | + q.removeFirst(); |
| 74 | + for (int k = matrix[j].length - 1; k > 0; k--) { |
| 75 | + if (matrix[j][k] == q.removeFirst()) // compare |
| 76 | + q.add(matrix[j][k]); |
| 77 | + else |
| 78 | + return false; |
| 79 | + } |
| 80 | + q.add(matrix[j][0]); |
| 81 | + } |
| 82 | + return true; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Solution when Only Load Data One Column at a Time |
| 87 | +class C { |
| 88 | + bool isToeplitzMatrix(List<List<int>> matrix) { |
| 89 | + int totalRows = matrix.length; |
| 90 | + int totalColumns = matrix[0].length; |
| 91 | + |
| 92 | + // Initiate the linked list and add the first column to the linked list. |
| 93 | + List<int> linkedList = []; |
| 94 | + for (int i = 0; i < totalRows; i++) linkedList.add(matrix[i][0]); |
| 95 | + |
| 96 | + for (int column = 1; column < totalColumns; column++) { |
| 97 | + // Check the column to see if any is not identical to the linked list elements. |
| 98 | + for (int row = 1; row < totalRows; row++) |
| 99 | + if (matrix[row][column] != linkedList[row - 1]) return false; |
| 100 | + // Update the linked list for the next line. |
| 101 | + linkedList.remove(linkedList.length - 1); |
| 102 | + linkedList.insert(0, matrix[0][column]); |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// |
| 109 | +class D { |
| 110 | + bool isToeplitzMatrix(List<List<int>> matrix) { |
| 111 | + if (matrix.length == 0 || matrix[0].length == 0) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + List<int> buffer = List.filled(matrix[0].length, 0); |
| 115 | + for (int j = 0; j < matrix[0].length; j++) { |
| 116 | + buffer[j] = matrix[0][j]; |
| 117 | + } |
| 118 | + for (int i = 1; i < matrix.length; i++) { |
| 119 | + for (int j = matrix[0].length - 1; j >= 1; j--) { |
| 120 | + if (buffer[j - 1] != matrix[i][j]) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + buffer[j] = matrix[i][j]; |
| 124 | + } |
| 125 | + buffer[0] = matrix[i][0]; |
| 126 | + } |
| 127 | + |
| 128 | + return true; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Load a partial row/column each time, the length of "piece" is defined as variable 'step' |
| 133 | +class E { |
| 134 | + int min(int a, int b) { |
| 135 | + return ((a > b) ? b : a); |
| 136 | + } |
| 137 | + |
| 138 | + int max(int a, int b) { |
| 139 | + return ((a < b) ? b : a); |
| 140 | + } |
| 141 | + |
| 142 | + bool isToeplitzMatrix(List<List<int>> matrix) { |
| 143 | + int width = matrix[0].length; |
| 144 | + int height = matrix.length; |
| 145 | + // This step indicates the maximum length of 'piece' which can be loaded at one time. |
| 146 | + int step = 3; |
| 147 | + int size = 1; |
| 148 | + int index = width - 1; |
| 149 | + |
| 150 | + while (index >= 0) { |
| 151 | + size = min(index + 1, step); |
| 152 | + List<int> memory = List.filled(size, 0); |
| 153 | + for (int i = 0; i < size; i++) { |
| 154 | + memory[size - i - 1] = matrix[0][index - i]; //set memory |
| 155 | + } |
| 156 | + for (int j = 1; j < min(height, width); j++) { |
| 157 | + //check the related pieces of rows |
| 158 | + //set boundary |
| 159 | + int rightBound = min(index + j, width - 1); |
| 160 | + int leftBound = max(index - step + 1 + j, j); |
| 161 | + for (int m = 0, n = leftBound; m < size && n <= rightBound; m++, n++) |
| 162 | + if (matrix[j][n] != memory[m]) return false; |
| 163 | + } |
| 164 | + index -= step; |
| 165 | + } |
| 166 | + |
| 167 | + index = 0; |
| 168 | + while (index < height) { |
| 169 | + //for the purpose of completeness, the criteria should include two sides of the matrix |
| 170 | + size = min(height - 1 - index, step); |
| 171 | + List<int> memory = List.filled(size, 0); |
| 172 | + for (int i = 0; i < size; i++) { |
| 173 | + memory[size - 1 - i] = matrix[height - index - 1 - i][0]; |
| 174 | + } |
| 175 | + for (int j = 1; j < min(height, width); j++) { |
| 176 | + int upperBound = max(height - index - step + j, j + 1); |
| 177 | + int lowerBound = min(height - index - 1 + j, height - 1); |
| 178 | + for (int m = 0, n = upperBound; m < size && n <= lowerBound; m++, n++) |
| 179 | + if (matrix[n][j] != memory[m]) return false; |
| 180 | + } |
| 181 | + index += step; |
| 182 | + } |
| 183 | + |
| 184 | + return true; |
| 185 | + } |
| 186 | +} |
0 commit comments