Skip to content

Commit c03f45d

Browse files
committed
leetcode
1 parent f2afd5b commit c03f45d

File tree

4 files changed

+380
-0
lines changed

4 files changed

+380
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
113113
- [Group Anagrams](GroupAnagrams/group_anagrams.dart)
114114
- [Earliest Possible Day of Full Bloom](EarliestPossibleDayOfFullBloom/earliest_possible_day_of_full_bloom.dart)
115115
- [Shortest Path in a Grid with Obstacles Elimination](ShortestPathInAGridWithObstaclesElimination/shortest_path_in_a_grid_with_obstacles_elimination.dart)
116+
- [Toeplitz Matrix](ToeplitzMatrix/toeplitz_matrix.dart)
116117

117118
## Reach me via
118119

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
}

ToeplitzMatrix/toeplitz_matrix.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
/*
4+
5+
bool isToeplitzMatrix(List<List<int>> matrix) {
6+
for (int i = 0; i < matrix.length - 1; i++) {
7+
for (int j = 0; j < matrix[0].length - 1; j++) {
8+
int ele = matrix[i][j];
9+
if (i < matrix.length - 1 &&
10+
j < matrix[0].length - 1 &&
11+
matrix[i + 1][j + 1] != ele) {
12+
return false;
13+
}
14+
}
15+
}
16+
return true;
17+
}
18+
*/
19+
func isToeplitzMatrix(matrix [][]int) bool {
20+
for i := 0; i < len(matrix)-1; i++ {
21+
for j := 0; j < len(matrix[0])-1; j++ {
22+
element := matrix[i][j]
23+
if i < len(matrix)-1 &&
24+
j < len(matrix[0])-1 &&
25+
matrix[i+1][j+1] != element {
26+
return false
27+
}
28+
}
29+
}
30+
return true
31+
}

ToeplitzMatrix/toeplitz_matrix.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# 🔥 Toeplitz Matrix 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## solution - 1 Brute Force
4+
5+
```dart
6+
class Solution {
7+
bool isToeplitzMatrix(List<List<int>> matrix) {
8+
for (int i = 0; i < matrix.length - 1; i++) {
9+
for (int j = 0; j < matrix[0].length - 1; j++) {
10+
int ele = matrix[i][j];
11+
if (i < matrix.length - 1 &&
12+
j < matrix[0].length - 1 &&
13+
matrix[i + 1][j + 1] != ele) {
14+
return false;
15+
}
16+
}
17+
}
18+
return true;
19+
}
20+
}
21+
```
22+
23+
## Solution - 2 Row by Row
24+
25+
```dart
26+
import 'dart:collection';
27+
28+
class Solution {
29+
bool isToeplitzMatrix(List<List<int>> matrix) {
30+
if (matrix.length <= 1 || matrix[0].length <= 1) return true;
31+
Queue<int> q = Queue();
32+
for (int i = matrix[0].length - 1; i >= 0; i--) {
33+
//set criteria
34+
q.add(matrix[0][i]);
35+
}
36+
for (int j = 1; j < matrix.length; j++) {
37+
q.removeFirst();
38+
for (int k = matrix[j].length - 1; k > 0; k--) {
39+
if (matrix[j][k] == q.removeFirst()) // compare
40+
q.add(matrix[j][k]);
41+
else
42+
return false;
43+
}
44+
q.add(matrix[j][0]);
45+
}
46+
return true;
47+
}
48+
}
49+
```
50+
51+
## Solution - 3 Column by Column
52+
53+
```dart
54+
class Solution {
55+
bool isToeplitzMatrix(List<List<int>> matrix) {
56+
int totalRows = matrix.length;
57+
int totalColumns = matrix[0].length;
58+
59+
// Initiate the linked list and add the first column to the linked list.
60+
List<int> linkedList = [];
61+
for (int i = 0; i < totalRows; i++) linkedList.add(matrix[i][0]);
62+
63+
for (int column = 1; column < totalColumns; column++) {
64+
// Check the column to see if any is not identical to the linked list elements.
65+
for (int row = 1; row < totalRows; row++)
66+
if (matrix[row][column] != linkedList[row - 1]) return false;
67+
// Update the linked list for the next line.
68+
linkedList.remove(linkedList.length - 1);
69+
linkedList.insert(0, matrix[0][column]);
70+
}
71+
return true;
72+
}
73+
}
74+
```
75+
76+
## Solution - 4
77+
78+
For the follow-up 1, we are only able to load one row at one time, so we have to use a buffer (1D data structure) to store the row info. When next row comes as a streaming flow, we can compare each value (say, `matrix[i][j], i>=1, j>=1`) to the "upper-left" value in the buffer (buffer[j - 1]); meanwhile, we update the buffer by inserting the 1st element of the current row (`matrix[i][0]`) to buffer at position 0 (buffer[0]), and removing the last element in the buffer.
79+
80+
```dart
81+
class Solution {
82+
bool isToeplitzMatrix(List<List<int>> matrix) {
83+
if (matrix.length == 0 || matrix[0].length == 0) {
84+
return true;
85+
}
86+
List<int> buffer = List.filled(matrix[0].length, 0);
87+
for (int j = 0; j < matrix[0].length; j++) {
88+
buffer[j] = matrix[0][j];
89+
}
90+
for (int i = 1; i < matrix.length; i++) {
91+
for (int j = matrix[0].length - 1; j >= 1; j--) {
92+
if (buffer[j - 1] != matrix[i][j]) {
93+
return false;
94+
}
95+
buffer[j] = matrix[i][j];
96+
}
97+
buffer[0] = matrix[i][0];
98+
}
99+
100+
return true;
101+
}
102+
}
103+
```
104+
105+
## Solution - 5
106+
107+
```dart
108+
class Solution {
109+
int min(int a, int b) {
110+
return ((a > b) ? b : a);
111+
}
112+
113+
int max(int a, int b) {
114+
return ((a < b) ? b : a);
115+
}
116+
117+
bool isToeplitzMatrix(List<List<int>> matrix) {
118+
int width = matrix[0].length;
119+
int height = matrix.length;
120+
// This step indicates the maximum length of 'piece' which can be loaded at one time.
121+
int step = 3;
122+
int size = 1;
123+
int index = width - 1;
124+
125+
while (index >= 0) {
126+
size = min(index + 1, step);
127+
List<int> memory = List.filled(size, 0);
128+
for (int i = 0; i < size; i++) {
129+
memory[size - i - 1] = matrix[0][index - i]; //set memory
130+
}
131+
for (int j = 1; j < min(height, width); j++) {
132+
//check the related pieces of rows
133+
//set boundary
134+
int rightBound = min(index + j, width - 1);
135+
int leftBound = max(index - step + 1 + j, j);
136+
for (int m = 0, n = leftBound; m < size && n <= rightBound; m++, n++)
137+
if (matrix[j][n] != memory[m]) return false;
138+
}
139+
index -= step;
140+
}
141+
142+
index = 0;
143+
while (index < height) {
144+
//for the purpose of completeness, the criteria should include two sides of the matrix
145+
size = min(height - 1 - index, step);
146+
List<int> memory = List.filled(size, 0);
147+
for (int i = 0; i < size; i++) {
148+
memory[size - 1 - i] = matrix[height - index - 1 - i][0];
149+
}
150+
for (int j = 1; j < min(height, width); j++) {
151+
int upperBound = max(height - index - step + j, j + 1);
152+
int lowerBound = min(height - index - 1 + j, height - 1);
153+
for (int m = 0, n = upperBound; m < size && n <= lowerBound; m++, n++)
154+
if (matrix[n][j] != memory[m]) return false;
155+
}
156+
index += step;
157+
}
158+
159+
return true;
160+
}
161+
}
162+
```

0 commit comments

Comments
 (0)