forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum-of-mutated-array-closest-to-target.py
68 lines (61 loc) · 1.94 KB
/
sum-of-mutated-array-closest-to-target.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def findBestValue(self, arr, target):
"""
:type arr: List[int]
:type target: int
:rtype: int
"""
arr.sort(reverse=True)
max_arr = arr[0]
while arr and arr[-1]*len(arr) <= target:
target -= arr.pop()
# let x = ceil(t/n)-1
# (1) (t/n-1/2) <= x:
# return x, which is equal to ceil(t/n)-1 = ceil(t/n-1/2) = (2t+n-1)//2n
# (2) (t/n-1/2) > x:
# return x+1, which is equal to ceil(t/n) = ceil(t/n-1/2) = (2t+n-1)//2n
# (1) + (2) => both return (2t+n-1)//2n
return max_arr if not arr else (2*target+len(arr)-1)//(2*len(arr))
# Time: O(nlogn)
# Space: O(1)
class Solution2(object):
def findBestValue(self, arr, target):
"""
:type arr: List[int]
:type target: int
:rtype: int
"""
arr.sort(reverse=True)
max_arr = arr[0]
while arr and arr[-1]*len(arr) <= target:
target -= arr.pop()
if not arr:
return max_arr
x = (target-1)//len(arr)
return x if target-x*len(arr) <= (x+1)*len(arr)-target else x+1
# Time: O(nlogm), m is the max of arr, which may be larger than n
# Space: O(1)
class Solution3(object):
def findBestValue(self, arr, target):
"""
:type arr: List[int]
:type target: int
:rtype: int
"""
def total(arr, v):
result = 0
for x in arr:
result += min(v, x)
return result
def check(arr, v, target):
return total(arr, v) >= target
left, right = 1, max(arr)
while left <= right:
mid = left + (right-left)//2
if check(arr, mid, target):
right = mid-1
else:
left = mid+1
return left-1 if target-total(arr, left-1) <= total(arr, left)-target else left