Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created November 4, 2019 14:37
Show Gist options
  • Save imedadel/0ddeea6803b0e3c8d7fd1962b13ae6d9 to your computer and use it in GitHub Desktop.
Save imedadel/0ddeea6803b0e3c8d7fd1962b13ae6d9 to your computer and use it in GitHub Desktop.
def candies(n, arr):
if len(arr) == 1:
return 1
candies = [1]*len(arr)
candies[0] = 1
for i in range(1, len(arr)):
if arr[i] > arr[i-1]:
candies[i] = candies[i] + candies[i-1]
for i in range(len(arr)-2, 0, -1):
if arr[i] > arr[i-1] and candies[i] <= candies[i+1]:
candies[i] = candies[i+1] + 1
return sum(candies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment