题目:给定一个有序的正数数组arr和一个正数range,如果可以自由选择arr中的数字,想累加得到1~range范围上所有的数,返回arr最少还缺几个数
举例:
arr = [1,2,3,7] range = 15 1~15还缺14 返回1
arr = [1,5,7] range = 15 1~15还缺2和4 返回2
def needMins(arr,range_):
range = 0
needs = 0
for i in range(len(arr)):
if arr[i] > range + 1:
range = 2 * range + 1
needs += 1
if range > range_:
return needs
range += arr[i]
if range > range_:
return needs
while range_ > range + 1:
range = 2 * range + 1
needs += 1
return needs