ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def hist(h):
st = []
best = 0
h = h + [0]
for i, x in enumerate(h):
start = i
while st and st[-1][1] >= x:
idx, height = st.pop()
best = max(best, height * (i - idx))
start = idx
st.append((start, x))
return best
print(hist([2, 1, 5, 6, 2, 3]), hist([2, 4]))- A. 10 4
- B. 8 4
- C. 10 2
- D. 12 4
Correct answer: A. 10 4
The best rectangle in [2, 1, 5, 6, 2, 3] spans the bars 5 and 6, giving 5 x 2 = 10. In [2, 4] the bar of height 4 alone gives 4.
More Python Programming questions
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…
- What is printed? python from collections import deque def knight(src, dst): seen = {src: 0} q = deque([src]) moves = [(1, 2), (2, 1), (-1…
- For an array of n = 10^5 values, which approach counts the distinct values in every window of size k = n / 2 in the least total time?
- What is printed? python a = [1, 2, 3, 4] try: print(a[len(a) / 2]) except TypeError: print('TypeError') print(a[len(a) // 2])
- What is printed? python s = 'u00b2' print(s.isdigit(), s.isdecimal(), s.isnumeric())
- What is printed? python d = {1: 'a'} d[1.0] = 'b' d[True] = 'c' print(d)