当前位置:网站首页>2022.02.28 - SX11-05. The largest rectangle in the histogram
2022.02.28 - SX11-05. The largest rectangle in the histogram
2022-06-12 15:41:00 【A CAI continues to work hard】
List of articles
1. subject


2. Ideas
(1) Monotonic stack
- left[i] Indicates subscript i The first height on the left is less than i The subscript ,right[i] Indicates subscript i The first height on the right is less than i The subscript .
- Use the subscript whose monotone stack height is less than the current subscript , Then the top element of the stack is the first subscript on the left or right that is smaller than the current subscript .
3. Code
import java.util.Deque;
import java.util.LinkedList;
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
Deque<Integer> stack = new LinkedList<>();
int[] left = new int[n];
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
stack.pop();
}
if (stack.isEmpty()) {
left[i] = -1;
} else {
left[i] = stack.peek();
}
stack.push(i);
}
stack = new LinkedList<>();
int[] right = new int[n];
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
stack.pop();
}
if (stack.isEmpty()) {
right[i] = n;
} else {
right[i] = stack.peek();
}
stack.push(i);
}
int res = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
res = Math.max(res, heights[i] * (right[i] - left[i] - 1));
}
return res;
}
}
边栏推荐
- Use of multithreading
- Rust tip - running the tensorrt model through FFI programming
- C language partition bin file program
- Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
- [untitled]
- Classic case of solidity - Smart games
- 安装rhel 7/8 (红帽)虚拟机(转载)
- Qiming Zhixian shares the application scheme of 2.8-inch handheld central control screen
- Change according to the situation, the road to promotion in the second half of 2022
- Unity get local video / download network video
猜你喜欢

Use of multithreading

UDP总结(TCP/IP详解卷1/2)
![[jvm learning] parental delegation mechanism and PC register (program counter)](/img/23/c7b1de7f648716c0f6b615dd22c64d.jpg)
[jvm learning] parental delegation mechanism and PC register (program counter)

IGMP message (tcp/ip details volume 1/ Volume 2)
![[LDA] basic knowledge notes - mainly AE and VAE](/img/1c/ccc073cac79b139becd5de0b9a91b1.png)
[LDA] basic knowledge notes - mainly AE and VAE

【光源实用案例】 UV-LED固化创新,让产线变得更丝滑

Raccourci vers le nouvel environnement du carnet de notes Jupiter

TF learning notes in ROS

5G新方案!升级现有的基站和UE模拟器至5G毫米波频段

Idea Encyclopedia (Reprinted)
随机推荐
Scala download and idea installation of scala plug-ins (nanny level tutorial is super detailed)
Distributed concurrent repeated submission
Codeworks round 797 (Div. 3, cf1690)
Solution of user and root forgetting password in virtual machine
The process of generating strong association rules from frequent itemsets
Interface.
How to set public IP access on the H3C gr5200 router
Learning is an inhumane thing (becoming an expert's internal mind skill)
Two ways of array simulating queue
Some useful websites
Idea pull branch code
VIM installation and common commands
Introduction to microservices
tinyint和int区别
ARM 64指令小记
How to write year-end summary
vim的安装以及常用命令
Classification of annotations
Pta: self test -3 array element cyclic right shift problem (20 points)
Task output: dense snow ice city theme song 0612