A warehouse has a row of box stacks. Every box is a cube with side length 1. Boxes are stacked from the floor upward without gaps. Each position is one unit wide, and adjacent positions touch without gaps.
You are given stackSizes, a list containing the number of boxes at each position from left to right. A value of 0 means that position is empty.
An empty unit space is hidden if there is at least one box somewhere to its left and at least one box somewhere to its right at the same height. These boxes do not need to be directly next to the space.
Consider empty unit spaces within the row, from the floor up to the top of the tallest stack. Empty spaces that are open to either end of the row do not count.
Return the total number of hidden empty unit spaces between the stacks.
int countHiddenSpaces(List<Integer> stackSizes)
stackSizes: the number of boxes at each position.1 ≤ stackSizes.size() ≤ 20,0000 ≤ stackSizes[i] ≤ 100,000Medium
Input: countHiddenSpaces(stackSizes = [5, 1, 3, 1, 4])
Output: 7
Explanation: The second, third, and fourth positions contain 3, 1, and 3 hidden empty spaces, respectively. Their total is 7.
Input: countHiddenSpaces(stackSizes = [4, 0, 2, 1, 5])
Output: 9
Explanation: The second, third, and fourth positions contain 4, 2, and 3 hidden empty spaces, respectively. Their total is 9.
Input: countHiddenSpaces(stackSizes = [5, 1, 4, 0, 2])
Output: 5
Explanation: There are 3 hidden empty spaces above the second position and 2 above the fourth position.
Input: countHiddenSpaces(stackSizes = [1, 2, 4, 6])
Output: 0
Explanation: Every empty space is open to the left end of the row.