@@ -72,32 +72,295 @@ tags:
72
72
73
73
<!-- solution:start -->
74
74
75
- ### 方法一
75
+ ### 方法一:动态规划 + 前后缀分解 + 枚举
76
+
77
+ 我们考虑将序列分成两部分,前 $k$ 个元素和后 $k$ 个元素,分别计算前后缀的所有可能的异或值。
78
+
79
+ 定义 $f[ i] [ j ] [ x] $ 表示前 $i$ 个元素中取 $j$ 个元素,是否存在一个子集的异或值为 $x$,定义 $g[ i] [ j ] [ y] $ 表示从下标 $i$ 开始取 $j$ 个元素,是否存在一个子集的异或值为 $y$。
80
+
81
+ 考虑 $f[ i] [ j ] [ x] $ 的转移方程,对于第 $i$ 个元素(从 $0$ 开始),我们可以选择不取,也可以选择取,因此有:
82
+
83
+ $$
84
+ f[i + 1][j][x] = f[i + 1][j][x] \lor f[i][j][x] \\
85
+ f[i + 1][j + 1][x \lor \text{nums}[i]] = f[i + 1][j + 1][x \lor \text{nums}[i]] \lor f[i][j][x]
86
+ $$
87
+
88
+ 对于 $g[ i] [ j ] [ y] $ 的转移方程,同样对于第 $i$ 个元素(从 $n - 1$ 开始),我们可以选择不取,也可以选择取,因此有:
89
+
90
+ $$
91
+ g[i - 1][j][y] = g[i - 1][j][y] \lor g[i][j][y] \\
92
+ g[i - 1][j + 1][y \lor \text{nums}[i - 1]] = g[i - 1][j + 1][y \lor \text{nums}[i - 1]] \lor g[i][j][y]
93
+ $$
94
+
95
+ 最后,我们在 $[ k, n - k] $ 的范围内枚举 $i$,对于每一个 $i$,我们枚举 $x$ 和 $y$,其中 $0 \leq x, y < 2^7$,如果 $f[ i] [ k ] [ x] $ 和 $g[ i] [ k ] [ y] $ 均为真,那么我们更新答案 $\text{ans} = \max(\text{ans}, x \oplus y)$。
96
+
97
+ 时间复杂度 $O(n \times m \times k)$,空间复杂度 $O(n \times m \times k)$,其中 $n$ 为数组长度,而 $m = 2^7$。
76
98
77
99
<!-- tabs:start -->
78
100
79
101
#### Python3
80
102
81
103
``` python
82
-
104
+ class Solution :
105
+ def maxValue (self , nums : List[int ], k : int ) -> int :
106
+ m = 1 << 7
107
+ n = len (nums)
108
+ f = [[[False ] * m for _ in range (k + 2 )] for _ in range (n + 1 )]
109
+ f[0 ][0 ][0 ] = True
110
+ for i in range (n):
111
+ for j in range (k + 1 ):
112
+ for x in range (m):
113
+ f[i + 1 ][j][x] |= f[i][j][x]
114
+ f[i + 1 ][j + 1 ][x | nums[i]] |= f[i][j][x]
115
+
116
+ g = [[[False ] * m for _ in range (k + 2 )] for _ in range (n + 1 )]
117
+ g[n][0 ][0 ] = True
118
+ for i in range (n, 0 , - 1 ):
119
+ for j in range (k + 1 ):
120
+ for y in range (m):
121
+ g[i - 1 ][j][y] |= g[i][j][y]
122
+ g[i - 1 ][j + 1 ][y | nums[i - 1 ]] |= g[i][j][y]
123
+
124
+ ans = 0
125
+ for i in range (k, n - k + 1 ):
126
+ for x in range (m):
127
+ if f[i][k][x]:
128
+ for y in range (m):
129
+ if g[i][k][y]:
130
+ ans = max (ans, x ^ y)
131
+ return ans
83
132
```
84
133
85
134
#### Java
86
135
87
136
``` java
88
-
137
+ class Solution {
138
+ public int maxValue (int [] nums , int k ) {
139
+ int m = 1 << 7 ;
140
+ int n = nums. length;
141
+ boolean [][][] f = new boolean [n + 1 ][k + 2 ][m];
142
+ f[0 ][0 ][0 ] = true ;
143
+
144
+ for (int i = 0 ; i < n; i++ ) {
145
+ for (int j = 0 ; j <= k; j++ ) {
146
+ for (int x = 0 ; x < m; x++ ) {
147
+ if (f[i][j][x]) {
148
+ f[i + 1 ][j][x] = true ;
149
+ f[i + 1 ][j + 1 ][x | nums[i]] = true ;
150
+ }
151
+ }
152
+ }
153
+ }
154
+
155
+ boolean [][][] g = new boolean [n + 1 ][k + 2 ][m];
156
+ g[n][0 ][0 ] = true ;
157
+
158
+ for (int i = n; i > 0 ; i-- ) {
159
+ for (int j = 0 ; j <= k; j++ ) {
160
+ for (int y = 0 ; y < m; y++ ) {
161
+ if (g[i][j][y]) {
162
+ g[i - 1 ][j][y] = true ;
163
+ g[i - 1 ][j + 1 ][y | nums[i - 1 ]] = true ;
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ int ans = 0 ;
170
+
171
+ for (int i = k; i <= n - k; i++ ) {
172
+ for (int x = 0 ; x < m; x++ ) {
173
+ if (f[i][k][x]) {
174
+ for (int y = 0 ; y < m; y++ ) {
175
+ if (g[i][k][y]) {
176
+ ans = Math . max(ans, x ^ y);
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ return ans;
184
+ }
185
+ }
89
186
```
90
187
91
188
#### C++
92
189
93
190
``` cpp
94
-
191
+ class Solution {
192
+ public:
193
+ int maxValue(vector<int >& nums, int k) {
194
+ int m = 1 << 7;
195
+ int n = nums.size();
196
+
197
+ vector<vector<vector<bool>>> f(n + 1, vector<vector<bool>>(k + 2, vector<bool>(m, false)));
198
+ f[0][0][0] = true;
199
+
200
+ for (int i = 0; i < n; i++) {
201
+ for (int j = 0; j <= k; j++) {
202
+ for (int x = 0; x < m; x++) {
203
+ if (f[i][j][x]) {
204
+ f[i + 1][j][x] = true;
205
+ f[i + 1][j + 1][x | nums[i]] = true;
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ vector<vector<vector<bool >>> g (n + 1, vector<vector<bool >>(k + 2, vector<bool >(m, false)));
212
+ g[ n] [ 0 ] [ 0] = true;
213
+
214
+ for (int i = n; i > 0; i--) {
215
+ for (int j = 0; j <= k; j++) {
216
+ for (int y = 0; y < m; y++) {
217
+ if (g[i][j][y]) {
218
+ g[i - 1][j][y] = true;
219
+ g[i - 1][j + 1][y | nums[i - 1]] = true;
220
+ }
221
+ }
222
+ }
223
+ }
224
+
225
+ int ans = 0;
226
+
227
+ for (int i = k; i <= n - k; i++) {
228
+ for (int x = 0; x < m; x++) {
229
+ if (f[i][k][x]) {
230
+ for (int y = 0; y < m; y++) {
231
+ if (g[i][k][y]) {
232
+ ans = max(ans, x ^ y);
233
+ }
234
+ }
235
+ }
236
+ }
237
+ }
238
+
239
+ return ans;
240
+ }
241
+ };
95
242
```
96
243
97
244
#### Go
98
245
99
246
```go
247
+ func maxValue(nums []int, k int) int {
248
+ m := 1 << 7
249
+ n := len(nums)
250
+
251
+ f := make([][][]bool, n+1)
252
+ for i := range f {
253
+ f[i] = make([][]bool, k+2)
254
+ for j := range f[i] {
255
+ f[i][j] = make([]bool, m)
256
+ }
257
+ }
258
+ f[0][0][0] = true
259
+
260
+ for i := 0; i < n; i++ {
261
+ for j := 0; j <= k; j++ {
262
+ for x := 0; x < m; x++ {
263
+ if f[i][j][x] {
264
+ f[i+1][j][x] = true
265
+ f[i+1][j+1][x|nums[i]] = true
266
+ }
267
+ }
268
+ }
269
+ }
270
+
271
+ g := make([][][]bool, n+1)
272
+ for i := range g {
273
+ g[i] = make([][]bool, k+2)
274
+ for j := range g[i] {
275
+ g[i][j] = make([]bool, m)
276
+ }
277
+ }
278
+ g[n][0][0] = true
279
+
280
+ for i := n; i > 0; i-- {
281
+ for j := 0; j <= k; j++ {
282
+ for y := 0; y < m; y++ {
283
+ if g[i][j][y] {
284
+ g[i-1][j][y] = true
285
+ g[i-1][j+1][y|nums[i-1]] = true
286
+ }
287
+ }
288
+ }
289
+ }
290
+
291
+ ans := 0
292
+
293
+ for i := k; i <= n-k; i++ {
294
+ for x := 0; x < m; x++ {
295
+ if f[i][k][x] {
296
+ for y := 0; y < m; y++ {
297
+ if g[i][k][y] {
298
+ ans = max(ans, x^y)
299
+ }
300
+ }
301
+ }
302
+ }
303
+ }
304
+
305
+ return ans
306
+ }
307
+ ```
100
308
309
+ #### TypeScript
310
+
311
+ ``` ts
312
+ function maxValue(nums : number [], k : number ): number {
313
+ const m = 1 << 7 ;
314
+ const n = nums .length ;
315
+
316
+ const f: boolean [][][] = Array .from ({ length: n + 1 }, () =>
317
+ Array .from ({ length: k + 2 }, () => Array (m ).fill (false )),
318
+ );
319
+ f [0 ][0 ][0 ] = true ;
320
+
321
+ for (let i = 0 ; i < n ; i ++ ) {
322
+ for (let j = 0 ; j <= k ; j ++ ) {
323
+ for (let x = 0 ; x < m ; x ++ ) {
324
+ if (f [i ][j ][x ]) {
325
+ f [i + 1 ][j ][x ] = true ;
326
+ f [i + 1 ][j + 1 ][x | nums [i ]] = true ;
327
+ }
328
+ }
329
+ }
330
+ }
331
+
332
+ const g: boolean [][][] = Array .from ({ length: n + 1 }, () =>
333
+ Array .from ({ length: k + 2 }, () => Array (m ).fill (false )),
334
+ );
335
+ g [n ][0 ][0 ] = true ;
336
+
337
+ for (let i = n ; i > 0 ; i -- ) {
338
+ for (let j = 0 ; j <= k ; j ++ ) {
339
+ for (let y = 0 ; y < m ; y ++ ) {
340
+ if (g [i ][j ][y ]) {
341
+ g [i - 1 ][j ][y ] = true ;
342
+ g [i - 1 ][j + 1 ][y | nums [i - 1 ]] = true ;
343
+ }
344
+ }
345
+ }
346
+ }
347
+
348
+ let ans = 0 ;
349
+
350
+ for (let i = k ; i <= n - k ; i ++ ) {
351
+ for (let x = 0 ; x < m ; x ++ ) {
352
+ if (f [i ][k ][x ]) {
353
+ for (let y = 0 ; y < m ; y ++ ) {
354
+ if (g [i ][k ][y ]) {
355
+ ans = Math .max (ans , x ^ y );
356
+ }
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ return ans ;
363
+ }
101
364
```
102
365
103
366
<!-- tabs:end -->
0 commit comments