不需要平均分
递归:
class Solution:
def balance(self, candidates):
res = []
cur = []
length = len(candidates)
for i in range(1, length+1):
self.back(candidates, 0, cur, res, i)
return min(res)
def back(self, candidates, start, cur, res, tar_length):
if len(cur) == tar_length:
print(cur)
res.append(abs(sum(candidates) - sum(cur) - sum(cur)))
return
for i in candidates[start:]:
cur.append(i)
self.back(candidates, candidates.index(i) + 1, cur, res, tar_length)
cur.pop()
print(Solution().balance([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))