File tree Expand file tree Collapse file tree 1 file changed +2
-29
lines changed Expand file tree Collapse file tree 1 file changed +2
-29
lines changed Original file line number Diff line number Diff line change @@ -295,64 +295,37 @@ Python3 Code:
295
295
296
296
297
297
298
+
298
299
` ` ` py
299
300
class Solution:
300
-
301
301
def minAbsDifference(self, nums: List[int], goal: int) -> int:
302
-
303
302
def combine_sum(A):
304
-
305
303
n = len(A)
306
-
307
304
dp = [0] * (1 << n)
308
-
309
305
for i in range(n):
310
-
311
306
for j in range(1 << i):
312
-
313
307
dp[(1 << i) + j] = dp[j] + A[i]
314
-
315
-
316
-
317
308
return dp
318
309
319
-
320
-
321
310
def combine_closest(c1, c2):
322
-
323
311
c1.sort()
324
-
325
312
c2.sort()
326
-
327
313
ans = float("inf")
328
-
329
314
i, j = 0, len(c2) - 1
330
-
331
315
while i < len(c1) and j >= 0:
332
-
333
316
_sum = c1[i] + c2[j]
334
-
335
317
ans = min(ans, abs(_sum - goal))
336
-
337
318
if _sum > goal:
338
-
339
319
j -= 1
340
-
341
320
elif _sum < goal:
342
-
343
321
i += 1
344
-
345
322
else:
346
-
347
323
return 0
348
-
349
324
return ans
350
325
351
-
352
-
353
326
n = len(nums)
354
-
355
327
return combine_closest(combine_sum(nums[: n // 2]), combine_sum(nums[n // 2 :]))
328
+
356
329
` ` `
357
330
358
331
** 复杂度分析**
You can’t perform that action at this time.
0 commit comments