forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candy.py
33 lines (27 loc) · 1 KB
/
candy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Time: O(n)
# Space: O(n)
#
# There are N children standing in a line. Each child is assigned a rating value.
#
# You are giving candies to these children subjected to the following requirements:
#
# Each child must have at least one candy.
# Children with a higher rating get more candies than their neighbors.
# What is the minimum candies you must give?
#
import operator
class Solution:
# @param ratings, a list of integer
# @return an integer
def candy(self, ratings):
candies = [1 for _ in xrange(len(ratings))]
for i in xrange(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
for i in reversed(xrange(1, len(ratings))):
if ratings[i - 1] > ratings[i] and candies[i - 1] <= candies[i]:
candies[i - 1] = candies[i] + 1
return reduce(operator.add, candies)
if __name__ == "__main__":
result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5])
print result