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

Commit e7c1224

Browse files
合并修改 0503.下一个更大元素II
2 parents a5eb340 + 2691e27 commit e7c1224

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

‎problems/0042.接雨水.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,56 @@ impl Solution {
975975
}
976976
```
977977

978+
Rust
979+
980+
双指针
981+
982+
```rust
983+
impl Solution {
984+
pub fn trap(height: Vec<i32>) -> i32 {
985+
let n = height.len();
986+
let mut max_left = vec![0; height.len()];
987+
let mut max_right = vec![0; height.len()];
988+
max_left.iter_mut().zip(max_right.iter_mut().rev()).enumerate().fold((0, 0), |(lm, rm), (idx, (x, y))| {
989+
let lmax = lm.max(height[idx]);
990+
let rmax = rm.max(height[n - 1 - idx]);
991+
*x = lmax; *y = rmax;
992+
(lmax, rmax)
993+
});
994+
height.iter().enumerate().fold(0, |acc, (idx, x)| {
995+
let h = max_left[idx].min(max_right[idx]);
996+
if h > 0 { h - x + acc } else { acc }
997+
})
998+
}
999+
}
1000+
```
1001+
1002+
单调栈
1003+
1004+
```rust
1005+
impl Solution {
1006+
pub fn trap(height: Vec<i32>) -> i32 {
1007+
let mut stack = vec![];
1008+
let mut ans = 0;
1009+
for (right_pos, &right_h) in height.iter().enumerate() {
1010+
while !stack.is_empty() && height[*stack.last().unwrap()] <= right_h {
1011+
let mid_pos = stack.pop().unwrap();
1012+
if !stack.is_empty() {
1013+
let left_pos = *stack.last().unwrap();
1014+
let left_h = height[left_pos];
1015+
let top = std::cmp::min(left_h, right_h);
1016+
if top > height[mid_pos] {
1017+
ans += (top - height[mid_pos]) * (right_pos - left_pos - 1) as i32;
1018+
}
1019+
}
1020+
}
1021+
stack.push(right_pos);
1022+
}
1023+
ans
1024+
}
1025+
}
1026+
```
1027+
9781028
<p align="center">
9791029
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
9801030
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0084.柱状图中最大的矩形.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,61 @@ impl Solution {
725725
}
726726
```
727727

728+
Rust
729+
730+
双指针预处理
731+
```rust
732+
733+
impl Solution {
734+
pub fn largest_rectangle_area(v: Vec<i32>) -> i32 {
735+
let n = v.len();
736+
let mut left_smaller_idx = vec![-1; n];
737+
let mut right_smaller_idx = vec![n as i32; n];
738+
for i in 1..n {
739+
let mut mid = i as i32 - 1;
740+
while mid >= 0 && v[mid as usize] >= v[i] {
741+
mid = left_smaller_idx[mid as usize];
742+
}
743+
left_smaller_idx[i] = mid;
744+
}
745+
for i in (0..n-1).rev() {
746+
let mut mid = i + 1;
747+
while mid < n && v[mid] >= v[i] {
748+
mid = right_smaller_idx[mid] as usize;
749+
}
750+
right_smaller_idx[i] = mid as i32;
751+
}
752+
let mut res = 0;
753+
for (idx, &e) in v.iter().enumerate() {
754+
res = res.max((right_smaller_idx[idx] - left_smaller_idx[idx] - 1) * e);
755+
}
756+
dbg!(res)
757+
}
758+
}
759+
```
760+
761+
单调栈
762+
```rust
763+
impl Solution {
764+
pub fn largest_rectangle_area1(mut v: Vec<i32>) -> i32 {
765+
v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值
766+
v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值
767+
let mut res = 0;
768+
let mut stack = vec![]; // 递增的栈
769+
for (idx, &e) in v.iter().enumerate() {
770+
while !stack.is_empty() && v[*stack.last().unwrap()] > e {
771+
let pos = stack.pop().unwrap();
772+
let prev_pos = *stack.last().unwrap();
773+
let s = (idx - prev_pos - 1) as i32 * v[pos];
774+
res = res.max(s);
775+
}
776+
stack.push(idx);
777+
}
778+
res
779+
}
780+
}
781+
```
782+
728783

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

0 commit comments

Comments
(0)

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