Data Structures & AlgorithmsVery hardCodeforces Div2 C/D · Amazon SDE OA
Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram (bars have width 1)? Run the brute-force code over all bar heights and extents.
Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram (bars have width 1)? Run the brute-force code over all bar heights and extents.
h=[2,1,5,6,2,3] n=len(h) print(max(min(h[i:j+1])*(j-i+1) for i in range(n) for j in range(i,n)))What is printed?
- A. 6
- B. 10
- C. 12
- D. 20
Correct answer: B. 10
A rectangle is a contiguous run of bars times its minimum height, so brute force over runs gives the answer. Twice the maximum bar is not a rectangle here, and using the suffix width ignores the bars that are shorter than the current one.
More Coding Interview Problems questions
- What is the maximum sum of a non-empty subarray in the circular array [8, -8, 9, -9, 10, -11, 12], where the subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?
- What is the minimum number of coins needed to make an amount of 18 using unlimited coins of denominations {1, 6, 10}?
- The array is [3, 3, 4, 4, 5, 1, 2, 2, 6, 3, 7]. What is the length of its longest non-decreasing (not strictly increasing) subsequence?
- For the circular array [5, -3, 5, -2, 6, -8, 4], what is the maximum sum of a non-empty contiguous subarray when the subarray is allowed…
- A robot moves only right or down from the top-left to the bottom-right cell of this 6×6 grid, where # is a blocked cell and . is free…