forked from maze-runnar/interview-preparation-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCandies.py
47 lines (34 loc) · 955 Bytes
/
Candies.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the candies function below.
def candies(n, arr):
minArr1 = [1 for i in range(n)]
minArr2 = [1 for i in range(n)]
finalArr = []
for i in range(1,len(arr)):
if arr[i] > arr[i-1]:
minArr1[i]=minArr1[i-1]+1
else:
minArr1[i] = 1
for j in range(len(arr)-2,-1,-1):
if arr[j] > arr[j+1]:
minArr2[j] = minArr2[j+1] +1
else:
minArr2[j] = 1
for i in range(len(arr)):
finalArr.append(max(minArr1[i], minArr2[i]))
return sum(finalArr)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = []
for _ in range(n):
arr_item = int(input())
arr.append(arr_item)
result = candies(n, arr)
fptr.write(str(result) + '\n')
fptr.close()