Skip to content

Commit 7db2033

Browse files
committed
feat: add algorithm description for 84 problem
1 parent dac390d commit 7db2033

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/hard/largest_rectangle_in_histogram.rs

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ pub fn largest_rectangle_area(heights: Vec<i32>) -> i32 {
3434
max_area
3535
}
3636

37+
/*
38+
Algorithm - Stack
39+
- Push index to stack if stack is empty or current height is greater than or equal to the height at index at top of stack
40+
- If current height is less than the height at index at top of stack, pop the index from stack and calculate area
41+
- Area = height[top] * (stack.is_empty() ? i : i - stack.top() - 1)
42+
- Update max_area
43+
- Pop all the indexes from stack and calculate area
44+
- Area = height[top] * (stack.is_empty() ? i : i - stack.top() - 1)
45+
- Update max_area
46+
- Return max_area
47+
48+
Time Complexity - O(n)
49+
Space Complexity - O(n)
50+
*/
51+
3752
#[cfg(test)]
3853
mod tests {
3954
use super::*;

0 commit comments

Comments
 (0)