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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| class Solution { public: vector<int> makeSubSeqSum(vector<int> nums) { int n = nums.size(); vector<int> ans(1 << n); for(int bits = (1 << n) - 1; bits >= 0; --bits) { int curState = bits; while(curState != 0) { int pos = ffs(curState) - 1; ans[bits] += nums[pos]; curState -= (1 << pos); } } return ans; }
void binSearchAdjTwoAndUpdate(int tar, int r, int goal, vector<int>& leftSet, int& minAbs) { auto it1 = lower_bound(leftSet.begin(), leftSet.end(), tar); if(it1 == leftSet.end()) { int l = *(it1 - 1); minAbs = min(abs(l + r - goal), minAbs); } else if(it1 == leftSet.begin()) { int l = *(it1); minAbs = min(abs(l + r - goal), minAbs); } else { int l1 = *it1; int l2 = *(it1 - 1); minAbs = min(abs(l1 + r - goal), minAbs); minAbs = min(abs(l2 + r - goal), minAbs); } }
int minAbsDifference(vector<int>& nums, int goal) { int n = nums.size(); int mid = n >> 1; auto leftSet = makeSubSeqSum(vector<int>{nums.begin(), nums.begin() + mid}); auto rightSet = makeSubSeqSum(vector<int>{nums.begin() + mid, nums.end()});
if(n == 1) { return min(abs(nums[0] - goal), abs(goal)); } sort(leftSet.begin(), leftSet.end()); sort(rightSet.begin(), rightSet.end());
int minAbs = INT_MAX;
int ptr1 = leftSet.size() - 1; int ptr2 = 0;
while(ptr1 >= 0 && ptr2 < rightSet.size()) { int curRes = leftSet[ptr1] + rightSet[ptr2]; minAbs = min(minAbs, abs(curRes - goal)); if(curRes < goal) { ++ptr2; } else if(curRes > goal) { --ptr1; } else { return 0; } }
return min(minAbs, abs(goal)); } };
|