LeetCode 题解工作台

构造最长非递减子数组

给你两个下标从 0 开始的整数数组 nums1 和 nums2 ,长度均为 n 。 让我们定义另一个下标从 0 开始、长度为 n 的整数数组, nums3 。对于范围 [0, n - 1] 的每个下标 i ,你可以将 nums1[i] 或 nums2[i] 的值赋给 nums3[i] 。 你的任务是…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们定义两个变量 和 ,分别表示当前位置的最长非递减子数组长度,其中 表示以 元素为结尾的最长非递减子数组长度,而 表示以 元素为结尾的最长非递减子数组长度。初始时 $f = g = 1$,初始答案 $ans = 1$。 接下来,我们在 $i \in [1, n)$ 的范围内遍历数组元素,对于每个 ,我们定义两个变量 和 ,分别表示以 和 元素为结尾的最长非递减子数组长度,初始化时…

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个下标从 0 开始的整数数组 nums1nums2 ,长度均为 n

让我们定义另一个下标从 0 开始、长度为 n 的整数数组,nums3 。对于范围 [0, n - 1] 的每个下标 i ,你可以将 nums1[i]nums2[i] 的值赋给 nums3[i]

你的任务是使用最优策略为 nums3 赋值,以最大化 nums3最长非递减子数组 的长度。

以整数形式表示并返回 nums3最长非递减 子数组的长度。

注意:子数组 是数组中的一个连续非空元素序列。

 

示例 1:

输入:nums1 = [2,3,1], nums2 = [1,2,1]
输出:2
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]
从下标 0 开始到下标 1 结束,形成了一个长度为 2 的非递减子数组 [2,2] 。 
可以证明 2 是可达到的最大长度。

示例 2:

输入:nums1 = [1,3,2,1], nums2 = [2,2,3,4]
输出:4
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]
整个数组形成了一个长度为 4 的非递减子数组,并且是可达到的最大长度。

示例 3:

输入:nums1 = [1,1], nums2 = [2,2]
输出:2
解释:构造 nums3 的方法之一是: 
nums3 = [nums1[0], nums1[1]] => [1,1] 
整个数组形成了一个长度为 2 的非递减子数组,并且是可达到的最大长度。

 

提示:

  • 1 <= nums1.length == nums2.length == n <= 105
  • 1 <= nums1[i], nums2[i] <= 109
lightbulb

解题思路

方法一:动态规划

我们定义两个变量 ffgg,分别表示当前位置的最长非递减子数组长度,其中 ff 表示以 nums1nums1 元素为结尾的最长非递减子数组长度,而 gg 表示以 nums2nums2 元素为结尾的最长非递减子数组长度。初始时 f=g=1f = g = 1,初始答案 ans=1ans = 1

接下来,我们在 i[1,n)i \in [1, n) 的范围内遍历数组元素,对于每个 ii,我们定义两个变量 ffffgggg,分别表示以 nums1[i]nums1[i]nums2[i]nums2[i] 元素为结尾的最长非递减子数组长度,初始化时 ff=gg=1ff = gg = 1

我们可以通过 ffgg 的值来计算出 ffffgggg 的值:

  • 如果 nums1[i]nums1[i1]nums1[i] \ge nums1[i - 1],那么 ff=max(ff,f+1)ff = \max(ff, f + 1)
  • 如果 nums1[i]nums2[i1]nums1[i] \ge nums2[i - 1],那么 ff=max(ff,g+1)ff = \max(ff, g + 1)
  • 如果 nums2[i]nums1[i1]nums2[i] \ge nums1[i - 1],那么 gg=max(gg,f+1)gg = \max(gg, f + 1)
  • 如果 nums2[i]nums2[i1]nums2[i] \ge nums2[i - 1],那么 gg=max(gg,g+1)gg = \max(gg, g + 1)

然后,我们更新 f=fff = ffg=ggg = gg,并将 ansans 更新为 max(ans,f,g)\max(ans, f, g)

遍历结束后,我们返回 ansans 即可。

时间复杂度 O(n)O(n),其中 nn 是数组的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)
        f = g = 1
        ans = 1
        for i in range(1, n):
            ff = gg = 1
            if nums1[i] >= nums1[i - 1]:
                ff = max(ff, f + 1)
            if nums1[i] >= nums2[i - 1]:
                ff = max(ff, g + 1)
            if nums2[i] >= nums1[i - 1]:
                gg = max(gg, f + 1)
            if nums2[i] >= nums2[i - 1]:
                gg = max(gg, g + 1)
            f, g = ff, gg
            ans = max(ans, f, g)
        return ans
speed

复杂度分析

指标
时间complexity is O(n) because each index is processed once and updates two states. Space complexity is O(n) for dp1 and dp2, or O(1) if using rolling variables instead of full arrays.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    They may hint at using DP and tracking two choices per index.

  • question_mark

    Expect questions about handling equal values or non-decreasing conditions.

  • question_mark

    They might ask about optimizing space from O(n) to O(1) using rolling variables.

warning

常见陷阱

外企场景
  • error

    Forgetting to consider both previous dp1 and dp2 values when updating the current state.

  • error

    Misinterpreting non-decreasing as strictly increasing.

  • error

    Not initializing the first element correctly, leading to off-by-one errors.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Find the longest non-decreasing subarray when more than two arrays are available to choose from.

  • arrow_right_alt

    Determine the maximum sum of the non-decreasing subarray instead of the length.

  • arrow_right_alt

    Compute the number of distinct longest non-decreasing subarrays constructible from two arrays.

help

常见问题

外企场景

构造最长非递减子数组题解:状态·转移·动态规划 | LeetCode #2771 中等