def largest_histogram(histogram): stack = list() max_area = 0 index = 0 while index < len(histogram): if (not stack) or (histogram[stack[-1]] <= histogram[index]): stack.append(index) index += 1 else: top_of_stack = stack.pop() area = (histogram[top_of_stack] * ((index - stack[-1] - 1) if stack else index)) max_area = max(max_area, area) while stack: top_of_stack = stack.pop() area = (histogram[top_of_stack] * ((index - stack[-1] - 1) if stack else index)) max_area = max(max_area, area) return max_area if __name__ == "__main__": # These "asserts" using only for self-checking and not necessary for auto-testing assert largest_histogram([5]) == 5, "one is always the biggest" assert largest_histogram([5, 3]) == 6, "two are smallest X 2" assert largest_histogram([1, 1, 4, 1]) == 4, "vertical" assert largest_histogram([1, 1, 3, 1]) == 4, "horizontal" assert largest_histogram([2, 1, 4, 5, 1, 3, 3]) == 8, "complex" print("Done! Go check it!")