-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathMinimum Sum of Squared Difference.py
43 lines (34 loc) · 1.41 KB
/
Minimum Sum of Squared Difference.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
# Runtime: 1506 ms (Top 29.6%) | Memory: 34.22 MB (Top 74.0%)
class Solution:
def minSumSquareDiff(self, nums1: List[int], nums2: List[int], k1: int, k2: int) -> int:
n = len(nums1)
k = k1+k2 # can combine k's because items can be turned negative
diffs = sorted((abs(x - y) for x, y in zip(nums1, nums2)))
# First binary search to find our new max for our diffs array
l, r = 0, max(diffs)
while l < r:
mid = (l+r)//2
# steps needed to reduce all nums greater than newMax
steps = sum(max(0, num-mid) for num in diffs)
if steps <= k:
r = mid
else:
l = mid+1
newMax = l
k -= sum(max(0, num-newMax) for num in diffs) # remove used k
# Second binary search to find first index to replace with max val
l, r = 0, n-1
while l < r:
mid = (l+r)//2
if diffs[mid] < newMax:
l = mid+1
else:
r = mid
# Replace items at index >= l with newMax
diffs = diffs[:l]+[newMax]*(n-l)
# Use remaining steps to reduce overall score
for i in range(len(diffs)-1,-1,-1):
if k == 0 or diffs[i] == 0: break
diffs[i] -= 1
k -= 1
return sum(diff*diff for diff in diffs)