Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 17 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e5d694d
Update
youngyangyang04 Jul 19, 2023
9b3c234
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
youngyangyang04 Jul 19, 2023
b3d1a88
Update 0503.下一个更大元素II.md
Ainevsia Jul 8, 2023
f3f549d
Update 0042.接雨水.md
Ainevsia Jul 8, 2023
7aa2a3e
Update 0084.柱状图中最大的矩形.md
Ainevsia Jul 9, 2023
0dc6bb5
添加59.螺旋矩阵II Ruby实现
niuli1991 Jul 25, 2023
7c9fcfe
更新 时间复杂度 O(n)超时 拍版格式修复
jinbudaily Jul 27, 2023
e0c5da7
更新 单调栈系列题目 排版格式修复
jinbudaily Jul 27, 2023
dc9fb7c
更新 数组额外题目 排版格式修复
jinbudaily Jul 27, 2023
56f3780
更新 哈希表额外题目 排版格式修复
jinbudaily Jul 27, 2023
bbb2a60
更新 二叉树额外题目 排版格式修复
jinbudaily Jul 27, 2023
e9b0d46
更新 贪心与动态规划 额外题目 排版格式修复
jinbudaily Jul 27, 2023
a5eb340
更新 图论 并查集 模拟 位运算 额外题目 排版格式修复
jinbudaily Jul 27, 2023
e7c1224
合并修改 0503.下一个更大元素II
jinbudaily Jul 27, 2023
9693ad4
Update
youngyangyang04 Jul 27, 2023
4037eb6
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
youngyangyang04 Jul 27, 2023
4f632c8
Merge pull request #2211 from jinbudaily/master
youngyangyang04 Jul 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0084.柱状图中最大的矩形.md
  • Loading branch information
Ainevsia authored and jinbudaily committed Jul 26, 2023
commit 7aa2a3efea391bc30047c31d17b179fcdb99221d
55 changes: 55 additions & 0 deletions problems/0084.柱状图中最大的矩形.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,61 @@ function largestRectangleArea(heights: number[]): number {
```


Rust

双指针预处理
```rust

impl Solution {
pub fn largest_rectangle_area(v: Vec<i32>) -> i32 {
let n = v.len();
let mut left_smaller_idx = vec![-1; n];
let mut right_smaller_idx = vec![n as i32; n];
for i in 1..n {
let mut mid = i as i32 - 1;
while mid >= 0 && v[mid as usize] >= v[i] {
mid = left_smaller_idx[mid as usize];
}
left_smaller_idx[i] = mid;
}
for i in (0..n-1).rev() {
let mut mid = i + 1;
while mid < n && v[mid] >= v[i] {
mid = right_smaller_idx[mid] as usize;
}
right_smaller_idx[i] = mid as i32;
}
let mut res = 0;
for (idx, &e) in v.iter().enumerate() {
res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e);
}
dbg!(res)
}
}
```

单调栈
```rust
impl Solution {
pub fn largest_rectangle_area1(mut v: Vec<i32>) -> i32 {
v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值
v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值
let mut res = 0;
let mut stack = vec![]; // 递增的栈
for (idx, &e) in v.iter().enumerate() {
while !stack.is_empty() && v[*stack.last().unwrap()] > e {
let pos = stack.pop().unwrap();
let prev_pos = *stack.last().unwrap();
let s = (idx - prev_pos - 1) as i32 * v[pos];
res = res.max(s);
}
stack.push(idx);
}
res
}
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

AltStyle によって変換されたページ (->オリジナル) /