Repository navigation
Expand file tree
/
Copy path0084_v2.cpp
More file actions
84 lines (71 loc) · 1.84 KB
/
Copy path0084_v2.cpp
File metadata and controls
84 lines (71 loc) · 1.84 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
// Leetcode 084 柱状图中最大的矩形
// https://www.codetd.com/article/3073438
#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;
}();
class Solution {
public:
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;
}
};
int main() {
string sin, rin;
vector<string> vs;
vector<vector<int>> vn;
vector<int> vr;
ifstream fp;
fp.open("0084.txt");
int d;
int c=0;
while (getline(fp, sin, '\n')) {
vs.push_back(sin);
stringstream ss(sin);
vector<int> vni = {};
while (ss >> d) {
vni.push_back(d);
// printf("%d ", d);
}
vn.push_back(vni);
getline(fp, rin, '\n');
vr.push_back(stoi(rin.c_str(),NULL,10));
// ++c;printf("==%d==\n", c);
}
fp.close();
printf("%s\t%s\t%s\t%s\n", "T","L", "√", "?");
printf("--- --- --- --- \n");
for (int i=0; i<vs.size(); ++i) {
int r = largestRectangleArea(vn[i]);
printf("%d\t%d\t%d\t%d\n", r==vr[i], 2*i+1, vr[i], r);
// for (int j=0; j<vn[i].size(); ++j) {
// printf("\n%d", vn[i][j]);
// }
}
return 0;
}