Skip to content

Commit 51c3502

Browse files
committed
refactor
1 parent f359c7c commit 51c3502

File tree

9 files changed

+7
-6
lines changed

9 files changed

+7
-6
lines changed
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

src/102_largest_rectangle_in_histogram/solution.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,26 @@ class Solution
3636
int largestRectangleArea(vector<int> &height)
3737
{
3838

39-
int ret = 0;
39+
int ans = 0;
4040
height.push_back(0);
4141
vector<int> position;
4242

4343
for (int i = 0; i < height.size(); i++)
4444
{
45-
while (position.size() > 0 && height[position.back()] >= height[i])
45+
while (position.size() > 0 && height[i] <= height[position.back()])
4646
{
4747
int h = height[position.back()];
4848
position.pop_back();
4949

50-
int sidx = position.size() > 0 ? position.back() : -1;
51-
if (h * (i - sidx - 1) > ret)
52-
ret = h * (i - sidx - 1);
50+
int idx_low = position.size() > 0 ? position.back() : -1;
51+
ans = max(ans, h * (i - idx_low + 1));
5352
}
53+
54+
// always push current index into stack, since it can be idx_low for future rectangle
5455
position.push_back(i);
5556
}
5657

57-
return ret;
58+
return ans;
5859
}
5960
};
6061

Binary file not shown.
Binary file not shown.
860 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)