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

给你一个整数数组 nums 。nums 的每个元素是 1,2 或 3。在每次操作中,你可以删除 nums 中的一个元素。返回使 nums 成为 非递减 顺序所需操作数的 最小值

 

示例 1:

输入:nums = [2,1,3,2,1]
输出:3
解释:
其中一个最优方案是删除 nums[0],nums[2] 和 nums[3]。

示例 2:

输入:nums = [1,3,2,1,3,3]
输出:2
解释:
其中一个最优方案是删除 nums[1] 和 nums[2]。

示例 3:

输入:nums = [2,2,2,2,3,3]
输出:0
解释:
nums 已是非递减顺序的。

 

提示:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 3

进阶:你可以使用 O(n) 时间复杂度以内的算法解决吗?

通过次数
10.3K
提交次数
16.2K
通过率
63.2%


相关企业

提示 1
The problem asks to change the array nums to make it sorted (i.e., all the 1s are on the left of 2s, and all the 2s are on the left of 3s.).

提示 2
We can try all the possibilities to make nums indices range in [0, i) to 0 and [i, j) to 1 and [j, n) to 2. Note the ranges are left-close and right-open; each might be empty. Namely, 0 <= i <= j <= n.

提示 3
Count the changes we need for each possibility by comparing the expected and original values at each index position.

评论 (0)

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