mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0084.柱状图中最大的矩形java动态规划解法
This commit is contained in:
@ -195,6 +195,40 @@ public:
|
||||
|
||||
Java:
|
||||
|
||||
动态规划
|
||||
```java
|
||||
class Solution {
|
||||
public int largestRectangleArea(int[] heights) {
|
||||
int length = heights.length;
|
||||
int[] minLeftIndex = new int [length];
|
||||
int[] maxRigthIndex = new int [length];
|
||||
|
||||
// 记录左边第一个小于该柱子的下标
|
||||
minLeftIndex[0] = -1 ;
|
||||
for (int i = 1; i < length; i++) {
|
||||
int t = i - 1;
|
||||
// 这里不是用if,而是不断向右寻找的过程
|
||||
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
|
||||
minLeftIndex[i] = t;
|
||||
}
|
||||
// 记录每个柱子 右边第一个小于该柱子的下标
|
||||
maxRigthIndex[length - 1] = length;
|
||||
for (int i = length - 2; i >= 0; i--) {
|
||||
int t = i + 1;
|
||||
while(t < length && heights[t] >= heights[i]) t = maxRigthIndex[t];
|
||||
maxRigthIndex[i] = t;
|
||||
}
|
||||
// 求和
|
||||
int result = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
int sum = heights[i] * (maxRigthIndex[i] - minLeftIndex[i] - 1);
|
||||
result = Math.max(sum, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
动态规划
|
||||
|
Reference in New Issue
Block a user