mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
Merge pull request #1726 from asiL-tcefreP/master
Update 0084.柱状图中最大的矩形.md (Java版本)
This commit is contained in:
@ -206,7 +206,7 @@ class Solution {
|
|||||||
public int largestRectangleArea(int[] heights) {
|
public int largestRectangleArea(int[] heights) {
|
||||||
int length = heights.length;
|
int length = heights.length;
|
||||||
int[] minLeftIndex = new int [length];
|
int[] minLeftIndex = new int [length];
|
||||||
int[] maxRigthIndex = new int [length];
|
int[] minRightIndex = new int [length];
|
||||||
// 记录左边第一个小于该柱子的下标
|
// 记录左边第一个小于该柱子的下标
|
||||||
minLeftIndex[0] = -1 ;
|
minLeftIndex[0] = -1 ;
|
||||||
for (int i = 1; i < length; i++) {
|
for (int i = 1; i < length; i++) {
|
||||||
@ -215,17 +215,17 @@ class Solution {
|
|||||||
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
|
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
|
||||||
minLeftIndex[i] = t;
|
minLeftIndex[i] = t;
|
||||||
}
|
}
|
||||||
// 记录每个柱子 右边第一个小于该柱子的下标
|
// 记录每个柱子右边第一个小于该柱子的下标
|
||||||
maxRigthIndex[length - 1] = length;
|
minRightIndex[length - 1] = length;
|
||||||
for (int i = length - 2; i >= 0; i--) {
|
for (int i = length - 2; i >= 0; i--) {
|
||||||
int t = i + 1;
|
int t = i + 1;
|
||||||
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
|
while(t < length && heights[t] >= heights[i]) t = minRightIndex[t];
|
||||||
maxRigthIndex[i] = t;
|
minRightIndex[i] = t;
|
||||||
}
|
}
|
||||||
// 求和
|
// 求和
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
|
int sum = heights[i] * (minRightIndex[i] - minLeftIndex[i] - 1);
|
||||||
result = Math.max(sum, result);
|
result = Math.max(sum, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
Reference in New Issue
Block a user