-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path084.rb
More file actions
30 lines (26 loc) · 681 Bytes
/
084.rb
File metadata and controls
30 lines (26 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# @param {Integer[]} heights
# @return {Integer}
def largest_rectangle_area(heights)
result = 0
past_heights = []
heights.each do |height|
if past_heights.length == 0 || past_heights[-1] <= height
past_heights.push(height)
else
count = 0
while past_heights.length > 0 && past_heights[-1] > height do
count += 1
h = past_heights.pop
result = h * count if h * count > result
end
(count + 1).times do
past_heights.push(height)
end
end
end
0.upto(past_heights.length - 1) do |i|
value = past_heights[i] * (past_heights.length - i)
result = value if value > result
end
result
end