Great job! I did the similar thing but failed to handle the desceding sequence properly. The way you treat the descending sequence as a whole and only add them & modify the peak once it ends is really nice.
Brilliant idea and great solution! The approach to divide the ratings array into multiple independent yet connected cycles is elegant. The extra one candy added when newsign is 0 is actually to complement the highest bar of the previous cycle. And actually we don't have to do early return for array size 1, treating it as the last cycle is also ok.
The idea is brilliant! Increasing candy count during downhill traverse, the first kid in downhill gets candy count which is supposed to give the last kid in downhill, while the last kid in downhill gets candy count which is supposed to give the first kid in downhill, however the total sum for downhill is still the same.
NO. Although there are two while, the second while is to find the whole up or down sequence starting from i. At the end of the inner loop i=j. There is no overlap between two while. The total iteration won't be great than n .