-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy.py
More file actions
27 lines (18 loc) · 668 Bytes
/
Copy pathcandy.py
File metadata and controls
27 lines (18 loc) · 668 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
25
26
27
# Leetcode mock interview - https://leetcode.com/problems/candy/
class Solution:
def candy(self, ratings: List[int]) -> int:
if len(ratings) == 1:
return 1
res = [1] * len(ratings)
for i in range(1, len(ratings)):
if ratings[i] > ratings[i-1]:
res[i] = res[i-1] + 1
# print(res, ratings)
for i in range(len(ratings)-2, -1, -1):
# print(i, res)
if ratings[i] > ratings[i+1]:
res[i] = max(res[i], res[i+1]+1)
# elif ratings[i] == ratings[i+1]:
# res[i] = res[i+1]
# print(res)
return sum(res)