-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximal_square.h
More file actions
executable file
·56 lines (47 loc) · 1.78 KB
/
Copy pathmaximal_square.h
File metadata and controls
executable file
·56 lines (47 loc) · 1.78 KB
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
53
54
55
56
/*
Author: cuckoo
Date: 2017/04/08 11:32:17
Update:
Problem: Maximal Square
Difficulty: Medium
Source: https://leetcode.com/problems/maximal-square/#/description
*/
#include <vector>
using std::vector;
#include <cmath> // for min(), max(), pow(), sqrt()
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
return maximalSquare_1(matrix);
}
int maximalSquare_1(vector<vector<char>>& matrix)
{
int size_row = matrix.size();
if(0 == size_row || 0 == matrix[0].size()) return 0;
int size_col = matrix[0].size();
// dp[row][col] represent the max-area from coordinate [row,col] to lower-right-corner of the matrix
vector<vector<int>> dp(size_row, vector<int>(size_col, 0));
for(int row = size_row - 1; row >= 0; --row)
dp[row][size_col-1] = matrix[row][size_col-1] == '0' ? 0 : 1;
for(int col = size_col - 1; col >= 0; --col)
dp[size_row-1][col] = matrix[size_row-1][col] == '0' ? 0 : 1;
for(int row = size_row - 2; row >= 0; --row)
{
for(int col = size_col - 2; col >= 0; --col)
{
if(matrix[row][col] == '1')
{
int min_area = std::min(dp[row+1][col+1], std::min(dp[row][col+1], dp[row+1][col]));
dp[row][col] = std::pow(std::sqrt(min_area) + 1, 2);
}
else
dp[row][col] = 0;
}
}
int result = 0;
for(auto &v : dp)
for(auto i : v)
result = std::max(result, i);
return result;
}
};