代码:
暴力解法(超时
- class Solution {
- public int largestRectangleArea(int[] heights) {
- int area = heights[0];
- int n = heights.length;
- for(int i=0;i
- int min = heights[i];
- for(int j=i;j
- min = Math.min(min,heights[j]);
- area = Math.max(area,heights[i]);
- area = Math.max(area,min*(j-i+1));
- }
- }
- return area;
- }
- }
题解里的 用栈存两边边界的数字
- class Solution {
- public int largestRectangleArea(int[] heights) {
- int n = heights.length;
- int[] left = new int[n];
- int[] right = new int[n];
- Deque
mono_stack = new ArrayDeque(); - for(int i=0;i
- while(!mono_stack.isEmpty()&&heights[mono_stack.peek()]>=heights[i]){
- mono_stack.pop();
- }
- left[i] = (mono_stack.isEmpty()?-1:mono_stack.peek());
- mono_stack.push(i);
- }
- mono_stack.clear();
- for(int i=n-1;i>=0;i--){
- while(!mono_stack.isEmpty()&&heights[mono_stack.peek()]>=heights[i]){
- mono_stack.pop();
- }
- right[i] = (mono_stack.isEmpty()?n:mono_stack.peek());
- mono_stack.push(i);
- }
- int ans = 0;
- for(int i=0;i
- ans = Math.max(ans,(right[i]-left[i]-1)*heights[i]);
- }
- return ans;
- }
- }
-
相关阅读:
第四章 数字逻辑电路设计方法【Verilog】
SpringCloud框架(二):整合Eureka作为注册中心、Feign进行远程调用、Ribbon实现负载均衡,底层源码解读
工业智能网关BL110应用之二十: 如何添加WAN口采集的设备
Redis缓存穿透,背八股文 居然没用!!!
【Buildroot】记一次编译出错gzip: popt-1.16.tar.gz: not in gzip format--更改br里面的默认下载地址
USB转串口芯片GP232RL 完全兼容替代FT232RL SSOP28
【JavaWeb项目】博客系统
序列式容器——vector
【面试宝藏】Redis 常见面试题解析
软考 系统架构设计师系列知识点之设计模式(2)
-
原文地址:https://blog.csdn.net/stacey777/article/details/134526382