⚠️ 改成每次只加一格呢
同样 16 次 append,但这次满了只把容量加一:
N = 16
def moves(n, grow):
"""模拟 n 次 append。返回 (一共搬了多少个元素, 单次最多搬几个)"""
cap, size, total, worst = 1, 0, 0, 0
for _ in range(n):
if size == cap:
total += size
worst = max(worst, size)
cap = grow(cap)
size += 1
return total, worst
total, worst = moves(N, lambda c: c + 1)
print(total)
全部评论