0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution: def findBestValue(self, arr: List[int], target: int) -> int: n = len(arr) arr.sort() pre = [0] for i in arr: pre.append(pre[-1]+i) diff = target ans = 0 for i in range(0,max(arr)+1): idx = bisect.bisect_left(arr,i) tempsum = pre[idx] + (n-idx)*i if abs(tempsum-target) < diff: ans = i diff = abs(tempsum-target) return ans
|