Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
A histogram has bar heights [2, 1, 5, 6, 2, 3, 4, 1] (each bar has width 1). What is the area of the largest rectangle that fits entirely inside the histogram?
- A. 10
- B. 12
- C. 24
- D. 11
Correct answer: A. 10
Keep a stack of increasing heights; when a bar is smaller than the stack top, pop and compute the rectangle that top bar spans. Taking the tallest bar times the full width, or a rectangle that ignores the bars on either side, gives a wrong area.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the sum of the minimum element over every contiguous subarray of [3, 1, 2, 4, 1]? (Every subarray counts, including repeated…
- Sum the maximum of every contiguous window of length 3 in the array [1, 3, -1, -3, 5, 3, 6, 7] (there are n − k + 1 windows). What is the…
- The stock span on day i is the number of consecutive days up to and including day i with price ≤ the price on day i. Prices are [100, 80…
- Bars of heights [4, 2, 0, 3, 2, 5, 1, 0, 3] (width 1 each) hold rainwater between them. How many units of water are trapped in total?
- Buildings of heights [5, 3, 8, 3, 2, 7, 9, 4] stand in a row viewed from the left. A building is visible if it is strictly taller than…
- The binary matrix [[1, 0, 1, 1, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]] is given. What is the area of the largest rectangle…