def solution(A):
cur = 0
prefix_sum = [0] * (len(A)+1)
for i, a in enumerate(A) :
prefix_sum[i+1] = a + prefix_sum [i]
m = (prefix_sum[2] - prefix_sum[0])/2
s = 0
for p1, p2, p3, i in zip(prefix_sum[:-3], prefix_sum[2:-1], prefix_sum[3:], range(len(prefix_sum[:-3]))):
if(p2-p1)/2 < m :
m = (p2-p1)/2
s = i
if (p3-p1)/3 < m :
m = (p3-p1)/3
s = i
return s
꼬리 안달고 해결하는 법이 뭐지..
def solution(A):
cur = 0
prefix_sum = [0] * (len(A)+1)
for i, a in enumerate(A) :
prefix_sum[i+1] = a + prefix_sum [i]
m = (prefix_sum[2] - prefix_sum[0])/2
s = 0
for p1, p2, p3, i in zip(prefix_sum, prefix_sum[2:], prefix_sum[3:], range(len(prefix_sum))):
if(p2-p1)/2 < m :
m = (p2-p1)/2
s = i
if (p3-p1)/3 < m :
m = (p3-p1)/3
s = i
if (A[-1]+A[-2])/2 < m :
s = len(A) -2
return s
그냥 Index 조절하는게 더 깔끔
def solution(A):
cur = 0
prefix_sum = [0] * (len(A)+1)
for i, a in enumerate(A) :
prefix_sum[i+1] = a + prefix_sum [i]
s=0
m=10E3
for i in range(len(prefix_sum)):
if i>1 and (prefix_sum[i]-prefix_sum[i-2])/2 <m:
m=(prefix_sum[i]-prefix_sum[i-2])/2
s=i-2
if i>2 and (prefix_sum[i] - prefix_sum[i-3])/3 < m:
m=(prefix_sum[i] - prefix_sum[i-3])/3
s=i-3
return s