赞
踩
10个球员准备分两队进行训练赛,教练希望两个队战斗力差值能尽可能小,以达到最佳的训练效果。
输入:
10个队员的战斗力,如[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
输出:
最小的战斗力差值,如:1
用递归解决:
class Solution: def balance(self, candidates): res = [] cur = [] self.back(candidates, 0, cur, res) return min(res) def back(self, candidates, start, cur, res): if len(cur) == 5: 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) cur.pop() print(Solution().balance([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。