Repository navigation
Expand file tree
/
Copy path0085.cpp
More file actions
107 lines (92 loc) · 2.47 KB
/
Copy path0085.cpp
File metadata and controls
107 lines (92 loc) · 2.47 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Maximum size rectangle binary sub-matrix with all 1s
// https://www.geeksforgeeks.org/maximum-size-rectangle-binary-sub-matrix-1s/
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
static const auto _ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
int largestRectangleArea(vector<int>& heights) {
heights.push_back(0); // Terminator
stack<int> st;
int max_area=0;
int ptr=0;
for (int i=0; i<heights.size(); ++i) {
if (st.empty() || heights[i] > heights[st.top()] ) {
st.push(i);
} else {
ptr = st.top();
st.pop();
max_area = max(max_area, heights[ptr]*(st.empty()?i:i-st.top()-1));
--i; // Important! To check all previous higher histogram.
}
}
return max_area;
}
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.size()==0)
return 0;
int max_area=0;
vector<int> row;
int rownum = matrix.size();
int colnum = matrix[0].size();
for (int j=0; j<colnum; ++j) {
row.push_back(0);
}
for (int i=0; i<rownum; ++i) {
for (int j=0; j<colnum; ++j) {
if (matrix[i][j]=='0')
row[j] = 0;
else
row[j] += 1;
}
max_area = max(max_area, largestRectangleArea(row));
}
return max_area;
}
};
int main() {
string sin;
int row, col, rr, state;
vector<vector<vector<char>>> vg={};
vector<int> vr;
ifstream fp;
fp.open("0085.txt");
while (getline(fp, sin, '\n')) {
stringstream ss(sin);
ss >> row >> col >> rr;
vr.push_back(rr);
vg.push_back({});
for (int i=0; i<row; ++i) {
getline(fp, sin, '\n');
stringstream ss(sin);
vg.back().push_back({});
for (int j=0; j<col; ++j) {
ss >> state;
vg.back()[i].push_back('0'+state);
}
}
}
fp.close();
printf("--- --- --- --- \n");
printf("%s\t%s\t%s\t%s\n", "T","L", "√", "?");
printf("--- --- --- --- \n");
int line=1;
for (int i=0; i<vr.size(); ++i) {
int res = maximalRectangle(vg[i]);
printf("%d\t%d\t%d\t%d\t\n", res==vr[i], line, vr[i], res);
line += vg[i].size()+1;
}
return 0;
}