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.
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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Candies
You are viewing a single comment's thread. Return to all 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