Question:codility Lesson17 MinAbsSum
My Answer:
def solution(A):
newA = [abs(i) for i in A]
totalsum = sum(newA)
#print(totalsum)
dic = {}
for ele in newA:
dic[ele] = dic.get(ele,0) + 1
reach = [0] + [-1] * (totalsum // 2)
for num in dic:
for numsum in range(totalsum // 2 + 1):
if reach[numsum] >= 0:
reach[numsum] = dic[num]
elif numsum >= num and reach[numsum - num] > 0:
reach[numsum] = reach[numsum - num] - 1
print(reach)
for ele in range(totalsum // 2,-1,-1):
if reach[ele] >= 0:
return totalsum - ele - ele
return 0