Skip to content

Commit b172d3c

Browse files
authored
Update 1755.Closest-Subsequence-Sum.cpp
1 parent 6ac6c7e commit b172d3c

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

Bit_Manipulation/1755.Closest-Subsequence-Sum/1755.Closest-Subsequence-Sum.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Solution {
2+
int ret = INT_MAX;
23
public:
34
int minAbsDifference(vector<int>& nums, int goal)
45
{
@@ -9,24 +10,9 @@ class Solution {
910

1011
vector<int>a = getSubSetsSum(nums1);
1112
vector<int>b = getSubSetsSum(nums2);
12-
13-
int ret = INT_MAX;
14-
for (auto x: a)
15-
{
16-
auto iter = lower_bound(b.begin(), b.end(), goal-x);
17-
if (iter!=b.end())
18-
ret = min(ret, abs(goal-x - *iter));
19-
if (iter!=b.begin())
20-
ret = min(ret, abs(goal-x - *prev(iter)));
21-
}
22-
for (auto x: b)
23-
{
24-
auto iter = lower_bound(a.begin(), a.end(), goal-x);
25-
if (iter!=a.end())
26-
ret = min(ret, abs(goal-x - *iter));
27-
if (iter!=a.begin())
28-
ret = min(ret, abs(goal-x - *prev(iter)));
29-
}
13+
14+
findAns(a,b,goal);
15+
findAns(b,a,goal);
3016
return ret;
3117
}
3218

@@ -82,4 +68,16 @@ class Solution {
8268
}
8369
return sums;
8470
}
71+
72+
void findAns(vector<int>&a, vector<int>&b, int goal)
73+
{
74+
for (auto x: a)
75+
{
76+
auto iter = lower_bound(b.begin(), b.end(), goal-x);
77+
if (iter!=b.end())
78+
ret = min(ret, abs(goal-x - *iter));
79+
if (iter!=b.begin())
80+
ret = min(ret, abs(goal-x - *prev(iter)));
81+
}
82+
}
8583
};

0 commit comments

Comments
 (0)