Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 688 Bytes

1300.-sum-of-mutated-array-closest-to-target.md

File metadata and controls

25 lines (21 loc) · 688 Bytes

1300. Sum of Mutated Array Closest to Target

class Solution:
    def findBestValue(self, arr: List[int], target: int) -> int:
        l, r = 0, max(arr)
        
        while l < r :
            mid = (l + r)//2
            sum1 = self.check(arr,mid)
            if sum1 < target:
                l = mid + 1
            else: ## 等于在这里, 所以 找到比目标大的 
                r = mid
        # print(r,l)
        s1 = self.check(arr,l)
        s2 = self.check(arr,l-1)
        if abs(s1-target) < abs(s2-target):
            return l
        return l-1
            
    def check(self,arr,t):
        return sum(map(lambda x : x if x <= t else t,arr))