1300. Sum of Mutated Array Closest to Target 链接到标题 对 arr 进行降序排序,将 arr 中的最小值乘以 arr 长度与 target比较,若比 target 小,则将其丢弃,并将其从 target 中减去。 若 arr 为空,则表示 arr 中最大值乘以 arr 长度仍比 target 小,则返回 arr 中的最大值。 若 arr 不为空,则表示 arr 中存在大于目标值的数值,取 target 除以此时 arr 长度(arr 中所有数值均大于目标值),最终值四舍五入得到目标值。
class Solution(object): def findBestValue(self, arr, target): """ :type arr: List[int] :type target: int :rtype: int """ arr.sort(reverse = True) while arr and target >= arr[-1]*len(arr): temp = arr[-1] target -= arr.