-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0135_candy.py
More file actions
24 lines (21 loc) · 732 Bytes
/
0135_candy.py
File metadata and controls
24 lines (21 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution(object):
def candy(self, ratings: list) -> int:
# Base Case
if len(ratings) == 1:
return 1
allocate = [1]
# Ascending order
for i in range(1, len(ratings)):
if ratings[i - 1] < ratings[i]:
allocate.append(allocate[i - 1] + 1)
else:
allocate.append(1)
# Descending order
for j in range(len(ratings) - 2, -1, -1):
if ratings[j] > ratings[j + 1] and allocate[j] <= allocate[j + 1]:
allocate[j] = allocate[j + 1] + 1
# Count number of candies
count = 0
for k in range(len(allocate)):
count += allocate[k]
return count