A histogram is made of a number of contiguous bars, which have same width. For a given…
- User
A histogram is made of a number of contiguous bars, which have same width.
For a given histogram with bars which have a width of 1 and a height of = respectively, find the area of the largest rectangular area.
Assistant
n = int(input()) h = list(map(int, input().split())) left_smaller = [-1] * n stack = [] for i in range(n): while stack and h[stack[-1]] >= h[i]: stack.pop() if stack: left_smaller[i] = stack[-1] else: left_smaller[i] = -1 stack.append(i) right_smaller = [n] * n stack = [] for i in range(n-1, -1, -1): while stack and h[stack[-1]] >= h[i]: stack.pop() if stack: right_smaller[i] = stack[-1] else: right_smaller[i] = n stack.append(i) max_area = 0 for i in range(n): width = right_smaller[i] - left_smaller[i] - 1 area = h[i] * width if area > max_area: max_area = area print(max_area)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.