题目地址:http://hero.pongo.cn/Question/Details?ID=58&ExamID=56
def initialize(array) @array = array end def largest_rectangle_area max_area = 0 @array.each_index do |i| right_blocks = @array[(i + 1)...@array.count] right_position = (right_blocks.index{|block_height| block_height < @array[i]} || right_blocks.count) + i left_blocks = @array[0...i] if left_position = left_blocks.rindex{|block_height| block_height < @array[i]} left_position += 1 else left_position = 0 end area_width = right_position - left_position + 1 area = area_width * @array[i] max_area = area if area > max_area end max_area end end describe Histogram do it "should be 10 if height array is [2,1,5,6,2,3] " do Histogram.new([2,1,5,6,2,3]).largest_rectangle_area.should == 10 end it "should be 8 if height array is [2,2,2,2] " do Histogram.new([2, 2, 2, 2]).largest_rectangle_area.should == 8 end it "should be 9 if height array is [1,2,3,4, 5] " do Histogram.new([1,2,3,4, 5]).largest_rectangle_area.should == 9 end end