8000 rectangle area · jhideki/rust_leetcode@42aa442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42aa442

Browse files
committed
rectangle area
1 parent 72422d7 commit 42aa442

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/largest_histogram.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub struct Solution {}
2+
3+
impl Solution {
4+
pub fn largest_area(heights: Vec<i32>) -> i32 {
5+
let mut largest_area: i32 = 0;
6+
let mut stack: Vec<(usize, i32)> = Vec::new();
7+
for i in 0..heights.len() {
8+
let mut start = i;
9+
while !stack.is_empty() && heights[i] < stack.last().unwrap().1 {
10+
let val = stack.pop().unwrap();
11+
let area = val.1 * (i - val.0) as i32;
12+
if area > largest_area {
13+
largest_area = area;
14+
}
15+
start = val.0;
16+
}
17+
stack.push((start, heights[i]));
18+
}
19+
20+
for val in stack {
21+
let area = (heights.len() - val.0) as i32 * val.1;
22+
if area > largest_area {
23+
largest_area = area;
24+
}
25+
}
26+
27+
largest_area
28+
}
29+
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
mod trapping_rain_water;
2-
use trapping_rain_water::Solution;
1+
mod largest_histogram;
2+
use largest_histogram::Solution;
33
fn main() {
4-
let heights = vec![0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1];
5-
let result = Solution::rain_water(heights);
4+
let heights = vec![2, 1, 5, 6, 2, 3];
5+
let result = Solution::largest_area(heights);
66
print!("{:?}", result);
77
}

0 commit comments

Comments
 (0)
0