-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1755.py
25 lines (25 loc) · 827 Bytes
/
1755.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
class Solution:
def minAbsDifference(self, nums: List[int], goal: int) -> int:
n = len(nums)
def state_compress(lst):
m = len(lst)
bit ={1<<i: lst[i] for i in range(m)}
dp = [0]*(1<<m)
for i in range(1,1<<m):
dp[i] = dp[i^i&-i] + bit[i&-i]
print(i^i&-i)
return sorted(list(set(dp)))
pre = state_compress(nums[:n//2])
post = state_compress(nums[n//2:])
ans = abs(goal)
i = 0
j = len(post)-1
while i < len(pre) and j >= 0:
ans = min(ans, abs(goal-pre[i]-post[j]))
if not ans:
return ans
if pre[i]+post[j] > goal:
j -= 1
elif pre[i]+post[j] < goal:
i += 1
return ans