-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcandy.py
More file actions
28 lines (21 loc) · 713 Bytes
/
candy.py
File metadata and controls
28 lines (21 loc) · 713 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
28
class Solution:
def candy(self, ratings: List[int]) -> int:
# Logic 1: O(N) iteration
n = len(ratings)
if n == 0:
return 0
if n == 1:
return 1
candies = [1]*n
for i in range(1, len(ratings)):
if ratings[i-1] < ratings[i]:
candies[i] = candies[i-1]+1
min_cost = candies[n-1]
for i in range(n-2, -1, -1):
if ratings[i] > ratings[i+1]:
v = candies[i+1]+1
if v > candies[i]:
candies[i] = v
min_cost += candies[i]
#print(candies)
return min_cost