LeetCode 题解工作台

求出数组中最大序列值

给你一个整数数组 nums 和一个 正 整数 k 。 定义长度为 2 * x 的序列 seq 的 值 为: (seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1]) . 请你…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们考虑将序列分成两部分,前 个元素和后 个元素,分别计算前后缀的所有可能的异或值。 定义 表示前 个元素中取 个元素,是否存在一个子集的异或值为 ,定义 表示从下标 开始取 个元素,是否存在一个子集的异或值为 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数数组 nums 和一个  整数 k 。

定义长度为 2 * x 的序列 seq 的  为:

  • (seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 * x - 1]).

请你求出 nums 中所有长度为 2 * k 的 子序列最大值 。

 

示例 1:

输入:nums = [2,6,7], k = 1

输出:5

解释:

子序列 [2, 7] 的值最大,为 2 XOR 7 = 5 。

示例 2:

输入:nums = [4,2,5,6,7], k = 2

输出:2

解释:

子序列 [4, 5, 6, 7] 的值最大,为 (4 OR 5) XOR (6 OR 7) = 2 。

 

提示:

  • 2 <= nums.length <= 400
  • 1 <= nums[i] < 27
  • 1 <= k <= nums.length / 2
lightbulb

解题思路

方法一:动态规划 + 前后缀分解 + 枚举

我们考虑将序列分成两部分,前 kk 个元素和后 kk 个元素,分别计算前后缀的所有可能的异或值。

定义 f[i][j][x]f[i][j][x] 表示前 ii 个元素中取 jj 个元素,是否存在一个子集的异或值为 xx,定义 g[i][j][y]g[i][j][y] 表示从下标 ii 开始取 jj 个元素,是否存在一个子集的异或值为 yy

考虑 f[i][j][x]f[i][j][x] 的转移方程,对于第 ii 个元素(从 00 开始),我们可以选择不取,也可以选择取,因此有:

f[i+1][j][x]=f[i+1][j][x]f[i][j][x]f[i+1][j+1][xnums[i]]=f[i+1][j+1][xnums[i]]f[i][j][x]f[i + 1][j][x] = f[i + 1][j][x] \lor f[i][j][x] \\ 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]

对于 g[i][j][y]g[i][j][y] 的转移方程,同样对于第 ii 个元素(从 n1n - 1 开始),我们可以选择不取,也可以选择取,因此有:

g[i1][j][y]=g[i1][j][y]g[i][j][y]g[i1][j+1][ynums[i1]]=g[i1][j+1][ynums[i1]]g[i][j][y]g[i - 1][j][y] = g[i - 1][j][y] \lor g[i][j][y] \\ 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]

最后,我们在 [k,nk][k, n - k] 的范围内枚举 ii,对于每一个 ii,我们枚举 xxyy,其中 0x,y<270 \leq x, y < 2^7,如果 f[i][k][x]f[i][k][x]g[i][k][y]g[i][k][y] 均为真,那么我们更新答案 ans=max(ans,xy)\text{ans} = \max(\text{ans}, x \oplus y)

时间复杂度 O(n×m×k)O(n \times m \times k),空间复杂度 O(n×m×k)O(n \times m \times k),其中 nn 为数组长度,而 m=27m = 2^7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution:
    def maxValue(self, nums: List[int], k: int) -> int:
        m = 1 << 7
        n = len(nums)
        f = [[[False] * m for _ in range(k + 2)] for _ in range(n + 1)]
        f[0][0][0] = True
        for i in range(n):
            for j in range(k + 1):
                for x in range(m):
                    f[i + 1][j][x] |= f[i][j][x]
                    f[i + 1][j + 1][x | nums[i]] |= f[i][j][x]

        g = [[[False] * m for _ in range(k + 2)] for _ in range(n + 1)]
        g[n][0][0] = True
        for i in range(n, 0, -1):
            for j in range(k + 1):
                for y in range(m):
                    g[i - 1][j][y] |= g[i][j][y]
                    g[i - 1][j + 1][y | nums[i - 1]] |= g[i][j][y]

        ans = 0
        for i in range(k, n - k + 1):
            for x in range(m):
                if f[i][k][x]:
                    for y in range(m):
                        if g[i][k][y]:
                            ans = max(ans, x ^ y)
        return ans
speed

复杂度分析

指标
时间complexity depends on iterating through all elements and possible k-length OR combinations, roughly O(n * k * 2^m) for bit states. Space complexity is O(n * k) for DP storage with optimization possible via rolling arrays.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    They may hint at optimizing bitwise operations rather than naive pair checking.

  • question_mark

    Expect prompts about state definition for dynamic programming and handling OR/XOR transitions.

  • question_mark

    Clarifications may focus on array length constraints and avoiding recomputation across subsequences.

warning

常见陷阱

外企场景
  • error

    Forgetting to consider all OR combinations leading to suboptimal maximum.

  • error

    Mismanaging DP state transitions causing index out-of-bounds errors.

  • error

    Assuming XOR distribution is linear without correctly pairing OR segments.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Compute maximum subsequence value with additional sum constraints alongside OR/XOR evaluation.

  • arrow_right_alt

    Allow subsequences of variable even lengths instead of strictly 2 * k.

  • arrow_right_alt

    Modify operations to include AND or custom bitwise operations while maintaining state transition DP.

help

常见问题

外企场景

求出数组中最大序列值题解:状态·转移·动态规划 | LeetCode #3287 困难