Skip to content

Commit 7c74bb5

Browse files
committed
completed candies
1 parent c853558 commit 7c74bb5

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

hackerrank/greedy/candies.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,20 @@
88

99
# Complete the candies function below.
1010
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)
3025

3126
if __name__ == '__main__':
3227
fptr = open(os.environ['OUTPUT_PATH'], 'w')

0 commit comments

Comments
 (0)