-
Notifications
You must be signed in to change notification settings - Fork 0
/
084_largest_rectangle_in_histogram.cpp
94 lines (90 loc) · 2.79 KB
/
084_largest_rectangle_in_histogram.cpp
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
/*******************************************************************************
* @File : 084_largest_rectangle_in_histogram.cpp
* @Author: Zhang Zixuan
* @Email : zixuan.zhang.victor@gmail.com
* @Blog : www.noathinker.com
* @Date : 2016年03月22日 星期二 17时02分59秒
******************************************************************************/
/*
* Question:
*
* Given n non-negative integers representing the histogram's bar height where
* the width of each bar is 1, find the area of largest rectangle in the histogram.
*
*/
class Solution {
public:
int largestArea(vector<int>& heights, int start, int end)
{
if (start > end)
return 0;
if (start == end)
return heights[start];
int minHeight = heights[start];
int minIndex = start;
bool sorted = true;
for (int i = start+1; i <= end; ++i)
{
if (heights[i] < heights[i-1])
sorted = false;
if (minHeight > heights[i])
{
minHeight = heights[i];
minIndex = i;
}
}
if (sorted)
{
int _max = 0;
for (int i = start; i <= end; i++) {
_max = max(_max, heights[i] * (end - i + 1));
}
return _max;
}
int maxArea = (end-start+1) * minHeight;
int leftIndex = minIndex-1;
while (start <= leftIndex && heights[leftIndex] == heights[minIndex])
leftIndex--;
int rightIndex = minIndex+1;
while (rightIndex <= end && heights[rightIndex] == heights[minIndex])
rightIndex++;
int leftArea = largestArea(heights, start, leftIndex);
int rightArea = largestArea(heights, rightIndex, end);
maxArea = max(maxArea, leftArea);
maxArea = max(maxArea, rightArea);
return maxArea;
}
/*
* Method1: O(n*logn) complecity
*/
int largestRectangleArea(vector<int>& heights) {
if (heights.size() == 0)
return 0;
int area = largestArea(heights, 0, heights.size()-1);
return area;
}
/*
* Method2: O(n^2) complecity
*/
int largestRectangleArea1(vector<int>& heights) {
if (heights.size() == 0)
return 0;
int start = 0;
int end = heights.size()-1;
int area = INT_MIN;
while (start <= end)
{
int height = heights[start];
for (int i = start+1; i <= end; ++i)
height = min(height, heights[i]);
area = max(area, height*(end-start+1));
if (start == end)
break;
if (heights[start] < heights[end])
start++;
else
end--;
}
return area;
}
};