문제링크

https://app.codility.com/programmers/lessons/3-time_complexity/tape_equilibrium/

 

TapeEquilibrium coding task - Learn to Code - Codility

Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.

app.codility.com

풀이

def solution(A):   
    first = A[0]
    second = sum(A[1:])
    res = []
    res.append(abs(first - second))
    for i in range(1, len(A)-1):
        first += A[i]
        second -= A[i]
        res.append(abs(first - second))
    return min(res)

여태 작성했던 방식이 모두 len(A)-1을 안 해서 생긴 오류라는걸 알게됐을때의 허무함...

두쪽으로 나눠야하니 리스트의 끝까지 계산하는 것이 아니라 리스트의 끝의 하나 앞에서 끊어준다.