84:柱状图中最大的矩形
This commit is contained in:
parent
b7fa41e1be
commit
42074b5a5d
@ -0,0 +1,72 @@
|
||||
//给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
|
||||
//
|
||||
// 求在该柱状图中,能够勾勒出来的矩形的最大面积。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// 输入: [2,1,5,6,2,3]
|
||||
//输出: 10
|
||||
// Related Topics 栈 数组
|
||||
// 👍 1383 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//84:柱状图中最大的矩形
|
||||
public class LargestRectangleInHistogram {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new LargestRectangleInHistogram().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int largestRectangleArea(int[] heights) {
|
||||
int length = heights.length;
|
||||
int[] left = new int[length];
|
||||
int[] right = new int[length];
|
||||
|
||||
Stack<Integer> stack = new Stack<Integer>();
|
||||
for (int i = 0; i < length; ++i) {
|
||||
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
|
||||
stack.pop();
|
||||
}
|
||||
left[i] = (stack.isEmpty() ? -1 : stack.peek());
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
stack.clear();
|
||||
for (int i = length - 1; i >= 0; --i) {
|
||||
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
|
||||
stack.pop();
|
||||
}
|
||||
right[i] = (stack.isEmpty() ? length : stack.peek());
|
||||
stack.push(i);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
ans = Math.max(ans, (right[i] - left[i] - 1) * heights[i]);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<p>给定 <em>n</em> 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。</p>
|
||||
|
||||
<p>求在该柱状图中,能够勾勒出来的矩形的最大面积。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/histogram.png"></p>
|
||||
|
||||
<p><small>以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 <code>[2,1,5,6,2,3]</code>。</small></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/histogram_area.png"></p>
|
||||
|
||||
<p><small>图中阴影部分为所能勾勒出的最大矩形面积,其面积为 <code>10</code> 个单位。</small></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> [2,1,5,6,2,3]
|
||||
<strong>输出:</strong> 10</pre>
|
||||
<div><div>Related Topics</div><div><li>栈</li><li>数组</li></div></div>\n<div><li>👍 1383</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user