classSolution:defmaxDistance(self, position: List[int], m:int)->int:# 判断当前的mid是否合法:存放的下m个球defcheck(x:int)->bool:
pre = position[0]
cnt =1for i inrange(1,len(position)):if position[i]- pre >= x:
pre = position[i]
cnt +=1return cnt >= m
position.sort()# 对位置预先排序
left, right, ans =1, position[-1]- position[0],-1while left <= right:
mid =(left + right)//2# 若合法,则考虑扩大左边界,寻找是否存在更大的最小磁力(合法值)if check(mid):
ans = mid # 更新更大的最小磁力
left = mid +1else:
right = mid -1return ans
classSolution:defmaxProfit(self, inventory: List[int], orders:int)->int:defcheck(ceil):
cnt =0for num in inventory:if num > ceil:
cnt += num - ceil
return cnt >= orders
# 确定卖之后剩余球的最大数量
low =0
high =max(inventory)while low < high -1:
mid = low +(high - low)//2if check(mid):
low = mid
else:
high = mid
rest = high if check(high)else low
rest +=1# 使用等差数列求和公式按照剩余的最大数量进行卖球
MOD =10**9+7
count =0
ans =0for num in inventory:if num > rest:
cur = num - rest
count += cur
ans += cur *(2* rest + cur +1)//2
ans %= MOD
# 若还不够数量则加上这部分价值
ans +=(orders - count)* rest
ans %= MOD
return ans