-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path135-Candy.py
More file actions
19 lines (16 loc) · 911 Bytes
/
Copy path135-Candy.py
File metadata and controls
19 lines (16 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Time Complexity: O(n)
class Solution:
def candy(self, ratings: List[int]) -> int:
result = [1] * len(ratings)
# Going rightward, check the index against it's leftward neighbor and add candy as needed
for i in range(1, len(ratings)):
if ratings[i-1] < ratings[i]:
result[i] = result[i-1] + 1
# Going leftward, check the index against it's rightward neighbor and add candy as needed
# Since values have already been populated, you must also check the edgecase in which
# more candy have been added to the cursor location than its rightward neighbor
for i in range (len(ratings) -2, -1, -1):
if ratings[i] > ratings[i+1]:
#Edge case in which the cursors rightward neighbor candy is smaller.
result[i] = max(result[i], result[i+1] +1)
return sum(result)