-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path221_maximal_square.h
52 lines (43 loc) · 1.33 KB
/
221_maximal_square.h
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef _47_221_MAXIMAL_SQUARE_H_
#define _47_221_MAXIMAL_SQUARE_H_
#include <iostream>
#include <vector> //std::vector
#include <algorithm> //std::min, std::max
using namespace std;
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
// m = # of rows; n = # of columns;
int m = matrix.size();
int n = matrix[0].size();
// value function dp[i][j] = size of max. square ending at point [i][j]
vector<vector<int>> dp(m, vector<int>(n, 0));
// running result
int max_sz = 0;
// initialize top-most row
for (int k = 0; k < n; k++) {
if (matrix[0][k] == '1') {
dp[0][k] = 1;
max_sz = 1;
}
}
// initialize left-most col
for (int k = 0; k < m; k++) {
if (matrix[k][0] == '1') {
dp[k][0] = 1;
max_sz = 1;
}
}
// loop through matrix
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
// update values
if (matrix[i][j] == '1')
dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
max_sz = max(max_sz, dp[i][j]);
}
}
return max_sz * max_sz;
}
};
#endif