-
Notifications
You must be signed in to change notification settings - Fork 0
/
Candy
30 lines (30 loc) · 1.19 KB
/
Candy
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
class Solution(object):
def candy(self, ratings):
"""
:type ratings: List[int]
:rtype: int
"""
if len(ratings)==1:
return 1
candies = [1]*len(ratings)
change = True
while change:
change = False
check = candies[:]
for i in range(len(ratings)):
if i == 0:
if ratings[i]>ratings[i+1]:
candies[i] = candies[i+1] + 1 if candies[i] <= candies[i+1] else candies[i]
change = True
elif i<len(ratings)-1:
if ratings[i]>ratings[i+1]:
candies[i] = candies[i+1] + 1 if candies[i] <= candies[i+1] else candies[i]
change = True
if ratings[i]>ratings[i-1]:
candies[i] = candies[i-1] + 1 if candies[i] <= candies[i-1] else candies[i]
change = True
else:
if ratings[i]>ratings[i-1]:
candies[i] = candies[i-1] + 1 if candies[i] <= candies[i-1] else candies[i]
change = True
return sum(candies)