调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
困难
相关标签
相关企业
提示

给你一个下标从 0 开始的整数数组 nums 。

你可以执行任意次操作。每次操作中,你需要选择一个 子数组 ,并将这个子数组用它所包含元素的  替换。比方说,给定数组是 [1,3,5,6] ,你可以选择子数组 [3,5] ,用子数组的和 8 替换掉子数组,然后数组会变为 [1,8,6] 。

请你返回执行任意次操作以后,可以得到的 最长非递减 数组的长度。

子数组 指的是一个数组中一段连续 非空 的元素序列。

 

示例 1:

输入:nums = [5,2,2]
输出:1
解释:这个长度为 3 的数组不是非递减的。
我们有 2 种方案使数组长度为 2 。
第一种,选择子数组 [2,2] ,对数组执行操作后得到 [5,4] 。
第二种,选择子数组 [5,2] ,对数组执行操作后得到 [7,2] 。
这两种方案中,数组最后都不是 非递减 的,所以不是可行的答案。
如果我们选择子数组 [5,2,2] ,并将它替换为 [9] ,数组变成非递减的。
所以答案为 1 。

示例 2:

输入:nums = [1,2,3,4]
输出:4
解释:数组已经是非递减的。所以答案为 4 。

示例 3:

输入:nums = [4,3,2,6]
输出:3
解释:将 [3,2] 替换为 [5] ,得到数组 [4,5,6] ,它是非递减的。
最大可能的答案为 3 。

 

提示:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105
通过次数
1.5K
提交次数
5.8K
通过率
26.4%


相关企业

提示 1
Let dp[i] be the maximum number of elements in the increasing sequence after processing the first i elements of the original array.

提示 2
We have dp[0] = 0. dp[i + 1] >= dp[i] (since if we have the solution for the first i elements, we can always merge the last one of the first i + 1 elements which is nums[i] into the solution of the first i elements.

提示 3
For i > 0, we want to dp[i] = max(dp[j] + 1) where sum(nums[i - 1] + nums[i - 2] +… + nums[j]) >= v[j] and v[j] is the last element of the solution ending with nums[j - 1].

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
nums =
[5,2,2]
Source