|
71 | 71 |
|
72 | 72 | <!-- 这里可写通用的实现逻辑 -->
|
73 | 73 |
|
| 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 | + |
74 | 90 | <!-- tabs:start -->
|
75 | 91 |
|
76 | 92 | ### **Python3**
|
77 | 93 |
|
78 | 94 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
79 | 95 |
|
80 | 96 | ```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) |
82 | 103 | ```
|
83 | 104 |
|
84 | 105 | ### **Java**
|
85 | 106 |
|
86 | 107 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
87 | 108 |
|
88 | 109 | ```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 | +``` |
89 | 187 |
|
| 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 | +} |
90 | 235 | ```
|
91 | 236 |
|
92 | 237 | ### **...**
|
|
0 commit comments