Skip to content

Commit 0a4ecd1

Browse files
committed
0084 solved.
1 parent 7a0cf14 commit 0a4ecd1

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
project(cpp_0084)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0084 main3.cpp)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/// Source : https://leetcode.com/problems/largest-rectangle-in-histogram/
2+
/// Author : liuyubobobo
3+
/// Time : 2020-05-13
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Divide and Conquer + Segment Tree
12+
/// Time Complexity: O(nlogn)
13+
/// Space Complexity: O(n)
14+
class SegmentTree{
15+
16+
private:
17+
int n;
18+
vector<int> data, tree;
19+
20+
public:
21+
SegmentTree(const vector<int>& data): data(data.begin(), data.end()), n(data.size()), tree(4*n, 0){
22+
buildSegTree(0, 0, n - 1);
23+
}
24+
25+
int query(int l, int r){
26+
return query(0, 0, n - 1, l, r);
27+
}
28+
29+
private:
30+
void buildSegTree(int treeID, int l, int r){
31+
32+
if(l == r){
33+
tree[treeID] = l;
34+
return;
35+
}
36+
37+
int mid = (l + r) / 2;
38+
buildSegTree(treeID * 2 + 1, l, mid);
39+
buildSegTree(treeID * 2 + 2, mid + 1, r);
40+
tree[treeID] = data[tree[treeID * 2 + 1]] < data[tree[treeID * 2 + 2]] ? tree[treeID * 2 + 1] : tree[treeID * 2 + 2];
41+
return;
42+
}
43+
44+
int query(int treeID, int l, int r, int ql, int qr){
45+
46+
if(ql == l && qr == r)
47+
return tree[treeID];
48+
49+
int mid = (l + r) / 2;
50+
if(qr <= mid) return query(treeID * 2 + 1, l, mid, ql, qr);
51+
else if(ql > mid) return query(treeID * 2 + 2, mid + 1, r, ql, qr);
52+
53+
int resl = query(treeID * 2 + 1, l, mid, ql, mid);
54+
int resr = query(treeID * 2 + 2, mid + 1, r, mid + 1, qr);
55+
return data[resl] < data[resr] ? resl : resr;
56+
}
57+
};
58+
59+
class Solution {
60+
61+
private:
62+
int n;
63+
64+
public:
65+
int largestRectangleArea(vector<int>& heights) {
66+
67+
n = heights.size();
68+
if(!n) return 0;
69+
70+
SegmentTree tree(heights);
71+
return largest(heights, 0, n - 1, tree);
72+
}
73+
74+
private:
75+
int largest(const vector<int>& heights, int l, int r, SegmentTree& tree){
76+
77+
if(l > r) return 0;
78+
if(l == r) return heights[l];
79+
80+
int min_index = tree.query(l, r);
81+
int res = largest(heights, l, min_index - 1, tree);
82+
res = max(res, largest(heights, min_index + 1, r, tree));
83+
res = max(res, heights[min_index] * (r - l + 1));
84+
return res;
85+
}
86+
};
87+
88+
89+
int main() {
90+
91+
vector<int> heights = {2,1,5,6,2,3};
92+
cout << Solution().largestRectangleArea(heights) << endl;
93+
// 10
94+
95+
return 0;
96+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/// Source : https://leetcode.com/problems/largest-rectangle-in-histogram/
2+
/// Author : liuyubobobo
3+
/// Time : 2020-05-13
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
12+
/// Monotone Stack
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
17+
public:
18+
int largestRectangleArea(vector<int>& heights) {
19+
20+
int n = heights.size();
21+
if(!n) return 0;
22+
23+
stack<int> stack;
24+
int res = 0;
25+
for(int i = 0; i < n; i ++){
26+
while(!stack.empty() && heights[i] < heights[stack.top()]){
27+
int x = stack.top();
28+
res = max(res, heights[x]);
29+
stack.pop();
30+
if(!stack.empty()) res = max(res, heights[x] * (i - 1 - (stack.top() + 1) + 1));
31+
else res = max(res, heights[x] * i);
32+
}
33+
stack.push(i);
34+
}
35+
36+
if(!stack.empty()){
37+
int r = stack.top();
38+
while(!stack.empty()){
39+
int x = stack.top();
40+
stack.pop();
41+
res = max(res, heights[x]);
42+
43+
if(!stack.empty()) res = max(res, heights[x] * (r - (stack.top() + 1) + 1));
44+
else res = max(res, heights[x] * n);
45+
}
46+
}
47+
return res;
48+
}
49+
};
50+
51+
52+
int main() {
53+
54+
vector<int> heights1 = {2,1,5,6,2,3};
55+
cout << Solution().largestRectangleArea(heights1) << endl;
56+
// 10
57+
58+
vector<int> heights2 = {2,1,2};
59+
cout << Solution().largestRectangleArea(heights2) << endl;
60+
// 3
61+
62+
vector<int> heights3 = {0, 9};
63+
cout << Solution().largestRectangleArea(heights3) << endl;
64+
// 9
65+
66+
vector<int> heights4 = {5, 4, 1, 2};
67+
cout << Solution().largestRectangleArea(heights4) << endl;
68+
// 8
69+
70+
vector<int> heights5 = {1, 2, 3, 4, 5};
71+
cout << Solution().largestRectangleArea(heights5) << endl;
72+
// 9
73+
74+
vector<int> heights6 = {4, 2, 0, 3, 2, 5};
75+
cout << Solution().largestRectangleArea(heights6) << endl;
76+
// 6
77+
78+
return 0;
79+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/// Source : https://leetcode.com/problems/largest-rectangle-in-histogram/
2+
/// Author : liuyubobobo
3+
/// Time : 2020-05-14
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
12+
/// Monotone Stack with sentinel to simplify the code
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
17+
public:
18+
int largestRectangleArea(vector<int>& heights) {
19+
20+
if(!heights.size()) return 0;
21+
22+
heights.insert(heights.begin(), 0);
23+
heights.push_back(0);
24+
25+
stack<int> stack;
26+
int res = 0;
27+
for(int i = 0; i < heights.size(); i ++){
28+
while(!stack.empty() && heights[i] < heights[stack.top()]){
29+
int x = stack.top();
30+
res = max(res, heights[x]);
31+
stack.pop();
32+
if(!stack.empty()) res = max(res, heights[x] * (i - 1 - (stack.top() + 1) + 1));
33+
else res = max(res, heights[x] * i);
34+
}
35+
stack.push(i);
36+
}
37+
return res;
38+
}
39+
};
40+
41+
42+
int main() {
43+
44+
vector<int> heights1 = {2,1,5,6,2,3};
45+
cout << Solution().largestRectangleArea(heights1) << endl;
46+
// 10
47+
48+
vector<int> heights2 = {2,1,2};
49+
cout << Solution().largestRectangleArea(heights2) << endl;
50+
// 3
51+
52+
vector<int> heights3 = {0, 9};
53+
cout << Solution().largestRectangleArea(heights3) << endl;
54+
// 9
55+
56+
vector<int> heights4 = {5, 4, 1, 2};
57+
cout << Solution().largestRectangleArea(heights4) << endl;
58+
// 8
59+
60+
vector<int> heights5 = {1, 2, 3, 4, 5};
61+
cout << Solution().largestRectangleArea(heights5) << endl;
62+
// 9
63+
64+
vector<int> heights6 = {4, 2, 0, 3, 2, 5};
65+
cout << Solution().largestRectangleArea(heights6) << endl;
66+
// 6
67+
68+
return 0;
69+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
128128
| | | | | | |
129129
| 082 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [] | [C++](0082-Remove-Duplicates-from-Sorted-List-II/cpp-0082/) | | |
130130
| 083 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [] | [C++](0083-Remove-Duplicates-from-Sorted-List/cpp-0083/) | | |
131+
| 084 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [solution](https://leetcode.com/problems/largest-rectangle-in-histogram/solution/) [题解](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/zhu-zhuang-tu-zhong-zui-da-de-ju-xing-by-leetcode/) | [C++](0084-Largest-Rectangle-in-Histogram/cpp-0084/) | | |
131132
| | | | | | |
132133
| 086 | [Partition List](https://leetcode.com/problems/partition-list/description/) | [solution](https://leetcode.com/problems/partition-list/solution/) | [C++](0086-Partition-List/cpp-0086/) | | |
133134
| 087 | [Scramble String](https://leetcode.com/problems/scramble-string/description/) | [] | [C++](0087-Scramble-String/cpp-0087/) | | |

0 commit comments

Comments
 (0)