What indices should be in the answer? In other words, what should I be looking for in order to solve the question?
The thing you should be looking for is: the index in the histogram, by whose height, the largest rectangle can be formed.
The reason is quite straightforward, the largest rectangle must be formed by a height of one of the heights, and you only have that much heights. Your mission is to loop for each of them, and see which is the largest. Which brings up the answer for your question #2.
Why do I need the index of the first bar to the left and right for each index? What does it serve?
To get the rectangle area formed by height at index i
, i.e., heights[i]
, you need to find the left boundary left
and right boundary right
, where left
< i
and right
> i
, and both heights[left - 1]
and heights[right + 1]
are smaller than heights[i]
. Because for indices, let denotes them as j
and k
, outside the two boundaries, the rectangle formed in the range [j
, k
] won't be formed by height[i]
.
Hope it helps resolve your confusion.