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.
Not sure what you are trying to do above, but here is a python version of my original solution. It passes all test cases.
def candies(n, arr):
s = [0]*n
c = [0]*n
for i in range(len(arr)):
if i == 0 or s[i-1] == 1:
s[i] = get_num_descending(arr, i)
else:
s[i] = s[i-1] - 1
c[i] = s[i] if arr[i] <= arr[i-1] else max(s[i], c[i-1]+1)
return sum(c)
def get_num_descending(arr, i):
'''
Returns the length of decreasing sequence, starting at position i.
'''
ret = 1
while i + 1 < len(arr):
if arr[i] > arr[i+1]:
ret += 1
i += 1
else:
return ret
return ret
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →
Not sure what you are trying to do above, but here is a python version of my original solution. It passes all test cases.