File tree 1 file changed +7
-5
lines changed
1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change 6
6
7
7
THESE ARE THE ALGORITHM'S RULES:
8
8
RULE 1: Scan `heights` array in left to right direction
9
- if the height at this index is greater than the height at the top position of `stack`
9
+ if the height at this index is greater than the height at the top position of `stack`
10
10
we push the index at `stack`.
11
11
12
12
RULE 2: If the `height` at the current `index` is smaller than the `height` at the top of the `stack`
13
- process the stack now. Pop top `index` from `stack`.
14
- The `height` of the rectangle = `height` at popped `index`.
15
- Calculate `width` of the rectangle:
16
- if the `stack` is not empty.
13
+ process the stack now. Pop top `index` from `stack`.
14
+ The `height` of the rectangle = `height` at popped `index`.
15
+ Calculate `width` of the rectangle:
16
+ if the `stack` is not empty.
17
17
Then the `width` extends from `begin` of the `histogram` till current `index`.
18
18
If the stack is not empty, the width stretches from a current index to an index that immediately comes after new stack top
19
19
31
31
32
32
__author__ = "Ansh Dulewale"
33
33
34
+
34
35
def largestRectangleArea (heights ):
35
36
stack = []
36
37
max_area = 0
@@ -46,6 +47,7 @@ def largestRectangleArea(heights):
46
47
heights .pop () # Restore the original heights array.
47
48
return max_area
48
49
50
+
49
51
# Get input from the user
50
52
user_input = input ()
51
53
heights = list (map (int , user_input .split ()))
You can’t perform that action at this time.
0 commit comments