forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargest_square_in_a_matrix_optimized.cpp
33 lines (32 loc) · 1.12 KB
/
largest_square_in_a_matrix_optimized.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <vector>
#include <algorithm>
int largestSquareInAMatrixOptimized(std::vector<std::vector<int>>& matrix) {
if (matrix.empty()) {
return 0;
}
int m = matrix.size();
int n = matrix[0].size();
std::vector<int> prevRow(n, 0);
int maxLen = 0;
// Iterate through the matrix.
for (int i = 0; i < m; i++) {
std::vector<int> currRow(n, 0);
for (int j = 0; j < n; j++) {
// Base cases: if we’re in row 0 or column 0, the largest square ending
// here has a length of 1. This can be set by using the value in the
// input matrix.
if (i == 0 || j == 0) {
currRow[j] = matrix[i][j];
} else {
if (matrix[i][j] == 1) {
// currRow[j] = 1 + min(left, top-left, top)
currRow[j] = 1 + std::min({currRow[j - 1], prevRow[j - 1], prevRow[j]});
}
}
maxLen = std::max(maxLen, currRow[j]);
}
// Update 'prevRow' with 'currRow' values for the next iteration.
prevRow = currRow;
}
return maxLen * maxLen;
}