forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest-subsequence-sum.cpp
53 lines (52 loc) · 1.86 KB
/
closest-subsequence-sum.cpp
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
// Time: O(n * 2^(n/2))
// Space: O(2^(n/2))
class Solution {
public:
int minAbsDifference(vector<int>& nums, int goal) {
int mx = 0, mn = 0;
for (const auto& x : nums) {
if (x > 0) {
mx += x;
} else if (x < 0) {
mn += x;
}
}
if (goal > mx) {
return goal - mx;
}
if (goal < mn) {
return mn - goal;
}
int result = abs(goal);
unordered_set<int> sums1{0};
for (auto i = 0; i < size(nums) / 2; ++i) {
for (const auto& x : vector<int>(cbegin(sums1), cend(sums1))) {
if (!sums1.emplace(x + nums[i]).second) {
continue;
}
result = min(result, abs(goal - x - nums[i])); // case of right half part is 0
}
}
vector<int> sorted_sums1(cbegin(sums1), cend(sums1)); // Space: O(2^(n/2))
sort(begin(sorted_sums1), end(sorted_sums1)); // Time: O((n/2) * 2^(n/2)) = O(n * 2^(n/2))
unordered_set<int> sums2{0};
for (int i = size(nums) / 2; i < size(nums); ++i) {
for (const auto& x : vector<int>(cbegin(sums2), cend(sums2))) {
if (!sums2.emplace(x + nums[i]).second) {
continue;
}
auto cit = lower_bound(cbegin(sorted_sums1), cend(sorted_sums1), goal - x - nums[i]); // Time: O(2^(n/2)) * O(n/2)
if (cit != cend(sorted_sums1)) {
result = min(result, abs(goal - x - nums[i] - *cit));
}
if (cit != cbegin(sorted_sums1)) {
result = min(result, abs(goal - x - nums[i] - *prev(cit)));
}
if (result == 0) {
return result;
}
}
}
return result;
}
};