blob: 988a62b49fa4a501f35ceff2058093925955b416 (
plain)
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
|
# 135. Candy
"""
there are 'n' children standing in a line. each child is assigned a rating
value. given in the integer array 'ratings'. 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 neighbours
return the maximum number of candies you need to have to distribute the
candies to the children
"""
class Solution(object):
def candy(self, ratings):
n = len(ratings)
candies = [1] * n
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
for i in range(n - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
return sum(candies)
if __name__ == "__main__":
obj = Solution()
print(obj.candy([1, 0, 2])) # expect: 5
print(obj.candy([1, 2, 2])) # expect: 4
|