File tree Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Original file line number Diff line number Diff line change @@ -177,21 +177,20 @@ class Solution {
177177``` python
178178class Solution :
179179 def candy (self , ratings : List[int ]) -> int :
180- candyVec = [1 ] * len (ratings)
180+ n = len (ratings)
181+ candies = [1 ] * n
181182
182- # 从前向后遍历,处理右侧比左侧评分高的情况
183- for i in range (1 , len (ratings) ):
183+ # Forward pass: handle cases where right rating is higher than left
184+ for i in range (1 , n ):
184185 if ratings[i] > ratings[i - 1 ]:
185- candyVec [i] = candyVec [i - 1 ] + 1
186+ candies [i] = candies [i - 1 ] + 1
186187
187- # 从后向前遍历,处理左侧比右侧评分高的情况
188- for i in range (len (ratings) - 2 , - 1 , - 1 ):
188+ # Backward pass: handle cases where left rating is higher than right
189+ for i in range (n - 2 , - 1 , - 1 ):
189190 if ratings[i] > ratings[i + 1 ]:
190- candyVec [i] = max (candyVec [i], candyVec [i + 1 ] + 1 )
191+ candies [i] = max (candies [i], candies [i + 1 ] + 1 )
191192
192- # 统计结果
193- result = sum (candyVec)
194- return result
193+ return sum (candies)
195194
196195```
197196
You can’t perform that action at this time.
0 commit comments