-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlice.py
28 lines (23 loc) · 1.2 KB
/
Slice.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
def solution(A):
min_avg_value = (A[0] + A[1]) / 2.0 # The mininal average
min_avg_pos = 0 # The begin position of the first
# slice with mininal average
for index in range(0, len(A) - 2):
# Try the next 2-element slice
if (A[index] + A[index + 1]) / 2.0 < min_avg_value:
min_avg_value = (A[index] + A[index + 1]) / 2.0
min_avg_pos = index
# Try the next 3-element slice
if (A[index] + A[index + 1] + A[index + 2]) / 3.0 < min_avg_value:
min_avg_value = (A[index] + A[index + 1] + A[index + 2]) / 3.0
min_avg_pos = index
# Try the last 2-element slice
if (A[-1] + A[-2]) / 2.0 < min_avg_value:
min_avg_value = (A[-1] + A[-2]) / 2.0
min_avg_pos = len(A) - 2
return min_avg_pos
print (solution([9,1,2,1,1]))
#A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N,
# is called a slice of array A (notice that the slice contains at least two elements).
# The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice.
# To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1).