• + 0 comments

    No additional array used

    def candies(n, arr): total = 1 peak_index=0 peak_value = 1 last_value = 1 i = 1 while iarr[j]: j +=1 i = j-1 j -=2 last_value = 1 total += last_value while j>=peak_index: if (arr[j]>arr[j+1]): last_value += 1 else: last_value=1 total += last_value j -= 1 total -= peak_value total -= last_value total += max(peak_value, last_value) last_value=1 i += 1

    return total