- 求和三分(底为3),确定可能的最低堆积高度
- 对源数组排序,分入三数组
- 以数组和差绝对值两两最小为准绳,适时调整最大和数组与最小数组和中的最大与最小元素
blocks = [2,2,6,5,4,6,3,5,4,6,2,2,6,5,4,2,2,6,5,4,6,3,5,4,6,2,2,6,5,4]
tong_size = 3
blocks.sort()
minH = sum(blocks)//3
print(sum(blocks),minH)
a,b,c = [],[],[]
for i in range(len(blocks)):
if sum(a)< minH:
a.append(blocks.pop())
elif sum(b)< minH:
b.append(blocks.pop())
else:
c = blocks
print(a,sum(a))
print(b,sum(b))
print(c,sum(c))
print("********")
# 将最大和的最大值元素与最小和最小元素交换
c.append(b.pop(0))
b.append(c.pop(0))
print(a,sum(a))
print(b,sum(b))
print(c,sum(c))
输出
124 41
[6, 6, 6, 6, 6, 6, 6] 42
[6, 5, 5, 5, 5, 5, 5, 4, 4] 44
[2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4] 38
********
[6, 6, 6, 6, 6, 6, 6] 42
[5, 5, 5, 5, 5, 5, 4, 4, 2] 40
[2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 6] 42
42 为三数组和差值最小时的高度