-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest_rectangle_in_histogram.rb
58 lines (48 loc) · 1.4 KB
/
largest_rectangle_in_histogram.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
# Example:
# heights = [2,1,5,6,2,3]
# Output: 10
# Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units.
def largest_rectangle_in_histogram(histogram)
s = []
left = []
histogram.each_with_index do |bar, index|
if s.empty?
s.push(index)
left.push(0 )
else
if histogram[s[-1]] > bar
s.pop
redo
else
left.push(s[-1] + 1)
s.push(index)
end
end
end
s = []
right = []
i = histogram.length - 1
while i >= 0 do
if s.empty?
s.push(i)
right.push(5)
else
if histogram[s[-1]] > histogram[i]
s.pop
redo
else
right.push(s[-1] - 1)
s.push(i)
end
end
i -= 1
end
max = 0
histogram.each_with_index do |bar, index|
m = (right.reverse[index] - left[index] + 1 ) * bar
max = m if m > max
end
max
end
puts largest_rectangle_in_histogram([2,1,5,6,2,3])