Skip to content

Commit 4429474

Browse files
committed
feat: add solutions to lc problem: No.1755
No.1755.Closest Subsequence Sum
1 parent 0fbf16f commit 4429474

File tree

5 files changed

+456
-0
lines changed

5 files changed

+456
-0
lines changed

solution/1700-1799/1755.Closest Subsequence Sum/README.md

+159
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:DFS + 二分查找**
56+
57+
每个数选或不选两种可能,所以 `n` 个数就有 `2^n` 种组合,由于 `n` 最大为 40,枚举 `2^40` 种组合显然会超时。
58+
59+
可以把数组分成左右两部分,分别求出两部分所有子序列和(`2 * 2^(n/2)` 种组合)记为 `lsum``rsum`。最后,只需找到最接近 `goal``lsum[i] + rsum[j]`
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
@@ -98,7 +104,160 @@ class Solution:
98104
<!-- 这里可写当前语言的特殊实现逻辑 -->
99105

100106
```java
107+
class Solution {
108+
public int minAbsDifference(int[] nums, int goal) {
109+
int n = nums.length;
110+
List<Integer> lsum = new ArrayList<>();
111+
List<Integer> rsum = new ArrayList<>();
112+
dfs(nums, lsum, 0, n / 2, 0);
113+
dfs(nums, rsum, n / 2, n, 0);
114+
115+
rsum.sort(Integer::compareTo);
116+
int res = Integer.MAX_VALUE;
117+
118+
for (Integer x : lsum) {
119+
int target = goal - x;
120+
int left = 0, right = rsum.size();
121+
while (left < right) {
122+
int mid = (left + right) >> 1;
123+
if (rsum.get(mid) < target) {
124+
left = mid + 1;
125+
} else {
126+
right = mid;
127+
}
128+
}
129+
if (left < rsum.size()) {
130+
res = Math.min(res, Math.abs(target - rsum.get(left)));
131+
}
132+
if (left > 0) {
133+
res = Math.min(res, Math.abs(target - rsum.get(left - 1)));
134+
}
135+
}
136+
137+
return res;
138+
}
139+
140+
private void dfs(int[] nums, List<Integer> sum, int i, int n, int cur) {
141+
if (i == n) {
142+
sum.add(cur);
143+
return;
144+
}
145+
146+
dfs(nums, sum, i + 1, n, cur);
147+
dfs(nums, sum, i + 1, n, cur + nums[i]);
148+
}
149+
}
150+
```
151+
152+
### **Go**
153+
154+
```go
155+
func minAbsDifference(nums []int, goal int) int {
156+
n := len(nums)
157+
lsum := make([]int, 0)
158+
rsum := make([]int, 0)
159+
160+
dfs(nums[:n/2], &lsum, 0, 0)
161+
dfs(nums[n/2:], &rsum, 0, 0)
162+
163+
sort.Ints(rsum)
164+
res := math.MaxInt32
165+
166+
for _, x := range lsum {
167+
t := goal - x
168+
l, r := 0, len(rsum)
169+
for l < r {
170+
m := int(uint(l+r) >> 1)
171+
if rsum[m] < t {
172+
l = m + 1
173+
} else {
174+
r = m
175+
}
176+
}
177+
if l < len(rsum) {
178+
res = min(res, abs(t-rsum[l]))
179+
}
180+
if l > 0 {
181+
res = min(res, abs(t-rsum[l-1]))
182+
}
183+
}
184+
185+
return res
186+
}
187+
188+
func dfs(nums []int, sum *[]int, i, cur int) {
189+
if i == len(nums) {
190+
*sum = append(*sum, cur)
191+
return
192+
}
193+
194+
dfs(nums, sum, i+1, cur)
195+
dfs(nums, sum, i+1, cur+nums[i])
196+
}
197+
198+
func min(x, y int) int {
199+
if x < y {
200+
return x
201+
}
202+
return y
203+
}
204+
205+
func abs(x int) int {
206+
if x < 0 {
207+
return -x
208+
}
209+
return x
210+
}
211+
```
101212

213+
### **C++**
214+
215+
```cpp
216+
class Solution {
217+
public:
218+
int minAbsDifference(vector<int>& nums, int goal) {
219+
int n = nums.size();
220+
vector<int> lsum;
221+
vector<int> rsum;
222+
dfs(nums, lsum, 0, n / 2, 0);
223+
dfs(nums, rsum, n / 2, n, 0);
224+
225+
sort(rsum.begin(), rsum.end());
226+
int res = INT_MAX;
227+
228+
for (int x : lsum) {
229+
int target = goal - x;
230+
int left = 0, right = rsum.size();
231+
while (left < right) {
232+
int mid = (left + right) >> 1;
233+
if (rsum[mid] < target) {
234+
left = mid + 1;
235+
} else {
236+
right = mid;
237+
}
238+
}
239+
if (left < rsum.size()) {
240+
res = min(res, abs(target - rsum[left]));
241+
}
242+
if (left > 0) {
243+
res = min(res, abs(target - rsum[left - 1]));
244+
}
245+
}
246+
247+
return res;
248+
}
249+
250+
private:
251+
void dfs(vector<int>& nums, vector<int>& sum, int i, int n, int cur) {
252+
if (i == n) {
253+
sum.emplace_back(cur);
254+
return;
255+
}
256+
257+
dfs(nums, sum, i + 1, n, cur);
258+
dfs(nums, sum, i + 1, n, cur + nums[i]);
259+
}
260+
};
102261
```
103262

104263
### **...**

solution/1700-1799/1755.Closest Subsequence Sum/README_EN.md

+153
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,160 @@ class Solution:
9191
### **Java**
9292

9393
```java
94+
class Solution {
95+
public int minAbsDifference(int[] nums, int goal) {
96+
int n = nums.length;
97+
List<Integer> lsum = new ArrayList<>();
98+
List<Integer> rsum = new ArrayList<>();
99+
dfs(nums, lsum, 0, n / 2, 0);
100+
dfs(nums, rsum, n / 2, n, 0);
101+
102+
rsum.sort(Integer::compareTo);
103+
int res = Integer.MAX_VALUE;
104+
105+
for (Integer x : lsum) {
106+
int target = goal - x;
107+
int left = 0, right = rsum.size();
108+
while (left < right) {
109+
int mid = (left + right) >> 1;
110+
if (rsum.get(mid) < target) {
111+
left = mid + 1;
112+
} else {
113+
right = mid;
114+
}
115+
}
116+
if (left < rsum.size()) {
117+
res = Math.min(res, Math.abs(target - rsum.get(left)));
118+
}
119+
if (left > 0) {
120+
res = Math.min(res, Math.abs(target - rsum.get(left - 1)));
121+
}
122+
}
123+
124+
return res;
125+
}
126+
127+
private void dfs(int[] nums, List<Integer> sum, int i, int n, int cur) {
128+
if (i == n) {
129+
sum.add(cur);
130+
return;
131+
}
132+
133+
dfs(nums, sum, i + 1, n, cur);
134+
dfs(nums, sum, i + 1, n, cur + nums[i]);
135+
}
136+
}
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func minAbsDifference(nums []int, goal int) int {
143+
n := len(nums)
144+
lsum := make([]int, 0)
145+
rsum := make([]int, 0)
146+
147+
dfs(nums[:n/2], &lsum, 0, 0)
148+
dfs(nums[n/2:], &rsum, 0, 0)
149+
150+
sort.Ints(rsum)
151+
res := math.MaxInt32
152+
153+
for _, x := range lsum {
154+
t := goal - x
155+
l, r := 0, len(rsum)
156+
for l < r {
157+
m := int(uint(l+r) >> 1)
158+
if rsum[m] < t {
159+
l = m + 1
160+
} else {
161+
r = m
162+
}
163+
}
164+
if l < len(rsum) {
165+
res = min(res, abs(t-rsum[l]))
166+
}
167+
if l > 0 {
168+
res = min(res, abs(t-rsum[l-1]))
169+
}
170+
}
171+
172+
return res
173+
}
174+
175+
func dfs(nums []int, sum *[]int, i, cur int) {
176+
if i == len(nums) {
177+
*sum = append(*sum, cur)
178+
return
179+
}
180+
181+
dfs(nums, sum, i+1, cur)
182+
dfs(nums, sum, i+1, cur+nums[i])
183+
}
184+
185+
func min(x, y int) int {
186+
if x < y {
187+
return x
188+
}
189+
return y
190+
}
191+
192+
func abs(x int) int {
193+
if x < 0 {
194+
return -x
195+
}
196+
return x
197+
}
198+
```
94199

200+
### **C++**
201+
202+
```cpp
203+
class Solution {
204+
public:
205+
int minAbsDifference(vector<int>& nums, int goal) {
206+
int n = nums.size();
207+
vector<int> lsum;
208+
vector<int> rsum;
209+
dfs(nums, lsum, 0, n / 2, 0);
210+
dfs(nums, rsum, n / 2, n, 0);
211+
212+
sort(rsum.begin(), rsum.end());
213+
int res = INT_MAX;
214+
215+
for (int x : lsum) {
216+
int target = goal - x;
217+
int left = 0, right = rsum.size();
218+
while (left < right) {
219+
int mid = (left + right) >> 1;
220+
if (rsum[mid] < target) {
221+
left = mid + 1;
222+
} else {
223+
right = mid;
224+
}
225+
}
226+
if (left < rsum.size()) {
227+
res = min(res, abs(target - rsum[left]));
228+
}
229+
if (left > 0) {
230+
res = min(res, abs(target - rsum[left - 1]));
231+
}
232+
}
233+
234+
return res;
235+
}
236+
237+
private:
238+
void dfs(vector<int>& nums, vector<int>& sum, int i, int n, int cur) {
239+
if (i == n) {
240+
sum.emplace_back(cur);
241+
return;
242+
}
243+
244+
dfs(nums, sum, i + 1, n, cur);
245+
dfs(nums, sum, i + 1, n, cur + nums[i]);
246+
}
247+
};
95248
```
96249

97250
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
int minAbsDifference(vector<int>& nums, int goal) {
4+
int n = nums.size();
5+
vector<int> lsum;
6+
vector<int> rsum;
7+
dfs(nums, lsum, 0, n / 2, 0);
8+
dfs(nums, rsum, n / 2, n, 0);
9+
10+
sort(rsum.begin(), rsum.end());
11+
int res = INT_MAX;
12+
13+
for (int x : lsum) {
14+
int target = goal - x;
15+
int left = 0, right = rsum.size();
16+
while (left < right) {
17+
int mid = (left + right) >> 1;
18+
if (rsum[mid] < target) {
19+
left = mid + 1;
20+
} else {
21+
right = mid;
22+
}
23+
}
24+
if (left < rsum.size()) {
25+
res = min(res, abs(target - rsum[left]));
26+
}
27+
if (left > 0) {
28+
res = min(res, abs(target - rsum[left - 1]));
29+
}
30+
}
31+
32+
return res;
33+
}
34+
35+
private:
36+
void dfs(vector<int>& nums, vector<int>& sum, int i, int n, int cur) {
37+
if (i == n) {
38+
sum.emplace_back(cur);
39+
return;
40+
}
41+
42+
dfs(nums, sum, i + 1, n, cur);
43+
dfs(nums, sum, i + 1, n, cur + nums[i]);
44+
}
45+
};

0 commit comments

Comments
 (0)