forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinAvgTwoSlice.py
More file actions
34 lines (23 loc) · 832 Bytes
/
MinAvgTwoSlice.py
File metadata and controls
34 lines (23 loc) · 832 Bytes
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
# link: https://codility.com/demo/take-sample-test/min_avg_two_slice
# name: Min Avg Two Slice
def solution(A):
# write your code in Python 2.6
n = len(A)
right = [0]*n
for index in xrange(1,n+1):
right[-index] = right[-index+1] + A[-index]
right.append(0)
solution = sum(A)/float(n)
result = 0
for index in xrange(0,n-1):
possible_solution = (right[index]-right[index+2])/float(2)
if possible_solution < solution:
solution = possible_solution
result = index
if index == n-2:
continue
possible_solution = (right[index]-right[index+3])/float(3)
if possible_solution < solution:
solution = possible_solution
result = index
return result