[codility] TapeEquilibrium (python)
Posted by 해랑쓰 블로그 (Haerang's blog)
문자열로 슬라이싱해서 sum으로 더해서 구하면 O(n^2)이 된다 또 한쪽의 길이가 0이 되도록 자르면 안되고 무조건 1개 이상의 원소는 포함해야 된다 하나는 하나만 포함하고 하나씩 수를 더해주는 반면 긴 부분에서는 앞에서부터 하나씩 빼서 차이를 구해주면 된다 timecomplexity: O(n) def solution(A): # write your code in Python 3.6 first=A[0] second=sum(A[1:]) res=abs(first-second) for i in range(1,len(A)-1): first+=A[i] second-=A[i] res=min(res,abs(first-second)) return res