Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created June 19, 2016 14:29
Show Gist options
  • Save maksverver/e5486785ab2abd4a8adf9d6b0822936b to your computer and use it in GitHub Desktop.
Save maksverver/e5486785ab2abd4a8adf9d6b0822936b to your computer and use it in GitHub Desktop.
Candies
# https://www.hackerrank.com/challenges/candies
import sys
N = int(sys.stdin.readline())
ratings = [ int(sys.stdin.readline()) for n in range(N) ]
candies = [1]*N
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
for i in reversed(range(N - 1)):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
print sum(candies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment