Skip to content

Commit a2d5c34

Browse files
committed
feat: add solutions to lc problem: No.1981
No.1981.Minimize the Difference Between Target and Chosen Elements
1 parent aaf6c36 commit a2d5c34

File tree

8 files changed

+379
-3
lines changed

8 files changed

+379
-3
lines changed

solution/1600-1699/1669.Merge In Between Linked Lists/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252

5353
直接模拟题目中的操作即可。
5454

55+
在实现上,我们使用两个指针 $p$ 和 $q$,初始时均指向链表 `list1` 的头节点。
56+
57+
然后我们向后移动指针 $p$ 和 $q$,直到指针 $p$ 指向链表 `list1` 中第 $a$ 个节点的前一个节点,指针 $q$ 指向链表 `list1` 中第 $b$ 个节点。此时我们将 $p$ 的 `next` 指针指向链表 `list2` 的头节点,将链表 `list2` 的尾节点的 `next` 指针指向 $q$ 的 `next` 指针指向的节点,即可完成题目要求。
58+
5559
时间复杂度 $O(m + n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为链表 `list1``list2` 的长度。
5660

5761
<!-- tabs:start -->

solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README.md

+146-1
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,167 @@
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74+
**方法一:动态规划(分组背包)**
75+
76+
设 $f[i][j]$ 表示前 $i$ 行是否能选出元素和为 $j$,则有状态转移方程:
77+
78+
$$
79+
f[i][j] = \begin{cases} 1 & \text{如果存在 } x \in row[i] \text{ 使得 } f[i - 1][j - x] = 1 \\ 0 & \text{否则} \end{cases}
80+
$$
81+
82+
其中 $row[i]$ 表示第 $i$ 行的元素集合。
83+
84+
由于 $f[i][j]$ 只与 $f[i - 1][j]$ 有关,因此我们可以使用滚动数组优化空间复杂度。
85+
86+
最后,遍历 $f$ 数组,找出最小的绝对差即可。
87+
88+
时间复杂度 $O(m^2 \times n \times C)$,空间复杂度 $O(m \times C)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数;而 $C$ 为矩阵元素的最大值。
89+
7490
<!-- tabs:start -->
7591

7692
### **Python3**
7793

7894
<!-- 这里可写当前语言的特殊实现逻辑 -->
7995

8096
```python
81-
97+
class Solution:
98+
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
99+
f = {0}
100+
for row in mat:
101+
f = set(a + b for a in f for b in row)
102+
return min(abs(v - target) for v in f)
82103
```
83104

84105
### **Java**
85106

86107
<!-- 这里可写当前语言的特殊实现逻辑 -->
87108

88109
```java
110+
class Solution {
111+
public int minimizeTheDifference(int[][] mat, int target) {
112+
Set<Integer> f = new HashSet<>();
113+
f.add(0);
114+
for (var row : mat) {
115+
Set<Integer> g = new HashSet<>();
116+
for (int a : f) {
117+
for (int b : row) {
118+
g.add(a + b);
119+
}
120+
}
121+
f = g;
122+
}
123+
int ans = 1 << 30;
124+
for (int v : f) {
125+
ans = Math.min(ans, Math.abs(v - target));
126+
}
127+
return ans;
128+
}
129+
}
130+
```
131+
132+
```java
133+
class Solution {
134+
public int minimizeTheDifference(int[][] mat, int target) {
135+
boolean[] f = {true};
136+
for (var row : mat) {
137+
int mx = 0;
138+
for (int x : row) {
139+
mx = Math.max(mx, x);
140+
}
141+
boolean[] g = new boolean[f.length + mx];
142+
for (int x : row) {
143+
for (int j = x; j < f.length + x; ++j) {
144+
g[j] |= f[j - x];
145+
}
146+
}
147+
f = g;
148+
}
149+
int ans = 1 << 30;
150+
for (int j = 0; j < f.length; ++j) {
151+
if (f[j]) {
152+
ans = Math.min(ans, Math.abs(j - target));
153+
}
154+
}
155+
return ans;
156+
}
157+
}
158+
```
159+
160+
### **C++**
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
166+
vector<int> f = {1};
167+
for (auto& row : mat) {
168+
int mx = *max_element(row.begin(), row.end());
169+
vector<int> g(f.size() + mx);
170+
for (int x : row) {
171+
for (int j = x; j < f.size() + x; ++j) {
172+
g[j] |= f[j - x];
173+
}
174+
}
175+
f = move(g);
176+
}
177+
int ans = 1 << 30;
178+
for (int j = 0; j < f.size(); ++j) {
179+
if (f[j]) {
180+
ans = min(ans, abs(j - target));
181+
}
182+
}
183+
return ans;
184+
}
185+
};
186+
```
89187
188+
### **Go**
189+
190+
```go
191+
func minimizeTheDifference(mat [][]int, target int) int {
192+
f := []int{1}
193+
for _, row := range mat {
194+
mx := 0
195+
for _, x := range row {
196+
mx = max(mx, x)
197+
}
198+
g := make([]int, len(f)+mx)
199+
for _, x := range row {
200+
for j := x; j < len(f)+x; j++ {
201+
g[j] |= f[j-x]
202+
}
203+
}
204+
f = g
205+
}
206+
ans := 1 << 30
207+
for j, v := range f {
208+
if v == 1 {
209+
ans = min(ans, abs(j-target))
210+
}
211+
}
212+
return ans
213+
}
214+
215+
func max(a, b int) int {
216+
if a > b {
217+
return a
218+
}
219+
return b
220+
}
221+
222+
func min(a, b int) int {
223+
if a < b {
224+
return a
225+
}
226+
return b
227+
}
228+
229+
func abs(x int) int {
230+
if x < 0 {
231+
return -x
232+
}
233+
return x
234+
}
90235
```
91236

92237
### **...**

solution/1900-1999/1981.Minimize the Difference Between Target and Chosen Elements/README_EN.md

+130-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,142 @@ The absolute difference is 1.
6464
### **Python3**
6565

6666
```python
67-
67+
class Solution:
68+
def minimizeTheDifference(self, mat: List[List[int]], target: int) -> int:
69+
f = {0}
70+
for row in mat:
71+
f = set(a + b for a in f for b in row)
72+
return min(abs(v - target) for v in f)
6873
```
6974

7075
### **Java**
7176

7277
```java
78+
class Solution {
79+
public int minimizeTheDifference(int[][] mat, int target) {
80+
Set<Integer> f = new HashSet<>();
81+
f.add(0);
82+
for (var row : mat) {
83+
Set<Integer> g = new HashSet<>();
84+
for (int a : f) {
85+
for (int b : row) {
86+
g.add(a + b);
87+
}
88+
}
89+
f = g;
90+
}
91+
int ans = 1 << 30;
92+
for (int v : f) {
93+
ans = Math.min(ans, Math.abs(v - target));
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
```java
101+
class Solution {
102+
public int minimizeTheDifference(int[][] mat, int target) {
103+
boolean[] f = {true};
104+
for (var row : mat) {
105+
int mx = 0;
106+
for (int x : row) {
107+
mx = Math.max(mx, x);
108+
}
109+
boolean[] g = new boolean[f.length + mx];
110+
for (int x : row) {
111+
for (int j = x; j < f.length + x; ++j) {
112+
g[j] |= f[j - x];
113+
}
114+
}
115+
f = g;
116+
}
117+
int ans = 1 << 30;
118+
for (int j = 0; j < f.length; ++j) {
119+
if (f[j]) {
120+
ans = Math.min(ans, Math.abs(j - target));
121+
}
122+
}
123+
return ans;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
134+
vector<int> f = {1};
135+
for (auto& row : mat) {
136+
int mx = *max_element(row.begin(), row.end());
137+
vector<int> g(f.size() + mx);
138+
for (int x : row) {
139+
for (int j = x; j < f.size() + x; ++j) {
140+
g[j] |= f[j - x];
141+
}
142+
}
143+
f = move(g);
144+
}
145+
int ans = 1 << 30;
146+
for (int j = 0; j < f.size(); ++j) {
147+
if (f[j]) {
148+
ans = min(ans, abs(j - target));
149+
}
150+
}
151+
return ans;
152+
}
153+
};
154+
```
73155
156+
### **Go**
157+
158+
```go
159+
func minimizeTheDifference(mat [][]int, target int) int {
160+
f := []int{1}
161+
for _, row := range mat {
162+
mx := 0
163+
for _, x := range row {
164+
mx = max(mx, x)
165+
}
166+
g := make([]int, len(f)+mx)
167+
for _, x := range row {
168+
for j := x; j < len(f)+x; j++ {
169+
g[j] |= f[j-x]
170+
}
171+
}
172+
f = g
173+
}
174+
ans := 1 << 30
175+
for j, v := range f {
176+
if v == 1 {
177+
ans = min(ans, abs(j-target))
178+
}
179+
}
180+
return ans
181+
}
182+
183+
func max(a, b int) int {
184+
if a > b {
185+
return a
186+
}
187+
return b
188+
}
189+
190+
func min(a, b int) int {
191+
if a < b {
192+
return a
193+
}
194+
return b
195+
}
196+
197+
func abs(x int) int {
198+
if x < 0 {
199+
return -x
200+
}
201+
return x
202+
}
74203
```
75204

76205
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minimizeTheDifference(vector<vector<int>>& mat, int target) {
4+
vector<int> f = {1};
5+
for (auto& row : mat) {
6+
int mx = *max_element(row.begin(), row.end());
7+
vector<int> g(f.size() + mx);
8+
for (int x : row) {
9+
for (int j = x; j < f.size() + x; ++j) {
10+
g[j] |= f[j - x];
11+
}
12+
}
13+
f = move(g);
14+
}
15+
int ans = 1 << 30;
16+
for (int j = 0; j < f.size(); ++j) {
17+
if (f[j]) {
18+
ans = min(ans, abs(j - target));
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func minimizeTheDifference(mat [][]int, target int) int {
2+
f := []int{1}
3+
for _, row := range mat {
4+
mx := 0
5+
for _, x := range row {
6+
mx = max(mx, x)
7+
}
8+
g := make([]int, len(f)+mx)
9+
for _, x := range row {
10+
for j := x; j < len(f)+x; j++ {
11+
g[j] |= f[j-x]
12+
}
13+
}
14+
f = g
15+
}
16+
ans := 1 << 30
17+
for j, v := range f {
18+
if v == 1 {
19+
ans = min(ans, abs(j-target))
20+
}
21+
}
22+
return ans
23+
}
24+
25+
func max(a, b int) int {
26+
if a > b {
27+
return a
28+
}
29+
return b
30+
}
31+
32+
func min(a, b int) int {
33+
if a < b {
34+
return a
35+
}
36+
return b
37+
}
38+
39+
func abs(x int) int {
40+
if x < 0 {
41+
return -x
42+
}
43+
return x
44+
}

0 commit comments

Comments
 (0)