File tree Expand file tree Collapse file tree 1 file changed +14
-19
lines changed Expand file tree Collapse file tree 1 file changed +14
-19
lines changed Original file line number Diff line number Diff line change 8
8
9
9
# Complete the candies function below.
10
10
def candies (n , arr ):
11
- candies = [None ] * n
12
- candies [0 ] = 1
13
-
14
- for i in range (1 , len (arr )):
15
- if arr [i ] >= arr [i - 1 ]:
16
- candies [i ] = candies [i - 1 ] + 1
17
- else :
18
- candies [i ] = 1
19
-
20
- cur_max = 1
21
- for i in reversed (range (0 , len (arr ))):
22
- if arr [i - 1 ] >= arr [i ] and cur_max < candies [i ]:
23
- candies [i ] = cur_max
24
- cur_max += 1
25
- else :
26
- candies [i ] = 1
27
-
28
- print (candies )
29
- return sum (candies )
11
+ candies = [None ] * n
12
+ candies [0 ] = 1
13
+
14
+ for i in range (1 , len (arr )):
15
+ if arr [i ] > arr [i - 1 ]:
16
+ candies [i ] = candies [i - 1 ] + 1
17
+ else :
18
+ candies [i ] = 1
19
+
20
+ for i in reversed (range (0 , len (arr ) - 1 )):
21
+ if arr [i ] > arr [i + 1 ] and candies [i ] <= candies [i + 1 ]:
22
+ candies [i ] = candies [i + 1 ] + 1
23
+
24
+ return sum (candies )
30
25
31
26
if __name__ == '__main__' :
32
27
fptr = open (os .environ ['OUTPUT_PATH' ], 'w' )
You can’t perform that action at this time.
0 commit comments