We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The editorial has the most inefficient solution to this problem. Three for loops--are you sure? This was my linear python3 solution (I tried to make it as readable as possible and in line with the vars defined in the problem's description):
This also exercises Linearity of Expectation, but all it's doing is sorting the input array y then walking through it to sum up v_i. If there's a repeating value, it'll use the first instance of v_i for that value (since v_i will continue to decrease as i increases).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Vertical Sticks
You are viewing a single comment's thread. Return to all comments →
The editorial has the most inefficient solution to this problem. Three for loops--are you sure? This was my linear python3 solution (I tried to make it as readable as possible and in line with the vars defined in the problem's description):
This also exercises Linearity of Expectation, but all it's doing is sorting the input array
y
then walking through it to sum upv_i
. If there's a repeating value, it'll use the first instance ofv_i
for that value (sincev_i
will continue to decrease asi
increases).