How to approach Vertical Sticks challenge
Imagine a different problem: if you had to place $k$ sticks of equal heights in $n$ slots then the expected distance between sticks (and the expected distance between the first stick and a notional slot $0$, and the expected distance between the last stick and a notional slot $n+1$) is $\frac{n+1}{k+1}$ since there are $k+1$ gaps to fit in a length $n+1$. 

Returning to this problem, a particular stick is interested in how many sticks (including itself) are as high or higher. If this number is $k$, then the expected gap to its left is also  $\frac{n+1}{k+1}$.

So the algorithm is simply to find this value for each stick and add up the expectation.  For example, starting with heights of $[3,2,5,3,3,4,1,2]$, the number of sticks with a greater or equal height is $[5,7,1,5,5,2,8,7]$ so the expectation is $\frac96+\frac98+\frac92+\frac96+\frac96+\frac93+\frac99+\frac98 = 15.25$.

This is easy to program: for example a single line in R

V &lt;- function(Y){ (length(Y) + 1) * sum( 1 / (rowSums(outer(Y, Y, "&lt;=")) + 1) ) }


gives the values in the sample output in the original problem 

&gt; V(c(1,2,3))
[1] 4.333333
&gt; V(c(3,3,3))
[1] 3
&gt; V(c(2,2,3))
[1] 4
&gt; V(c(10,2,4,4))
[1] 6
&gt; V(c(10,10,10,5,10))
[1] 5.8
&gt; V(c(1,2,3,4,5,6))
[1] 11.15