comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
困难 |
1912 |
第 40 场双周赛 Q4 |
|
我们定义 arr
是 山形数组 当且仅当它满足:
arr.length >= 3
- 存在某个下标
i
(从 0 开始) 满足0 < i < arr.length - 1
且:arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
给你整数数组 nums
,请你返回将 nums
变成 山形状数组 的 最少 删除次数。
示例 1:
输入:nums = [1,3,1] 输出:0 解释:数组本身就是山形数组,所以我们不需要删除任何元素。
示例 2:
输入:nums = [2,1,1,5,6,2,3,1] 输出:3 解释:一种方法是将下标为 0,1 和 5 的元素删除,剩余元素为 [1,5,6,3,1] ,是山形数组。
提示:
3 <= nums.length <= 1000
1 <= nums[i] <= 109
- 题目保证
nums
删除一些元素后一定能得到山形数组。
本题可以转化为求最长上升子序列和最长下降子序列。
我们定义
那么最终答案就是
时间复杂度
class Solution:
def minimumMountainRemovals(self, nums: List[int]) -> int:
n = len(nums)
left = [1] * n
right = [1] * n
for i in range(1, n):
for j in range(i):
if nums[i] > nums[j]:
left[i] = max(left[i], left[j] + 1)
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
if nums[i] > nums[j]:
right[i] = max(right[i], right[j] + 1)
return n - max(a + b - 1 for a, b in zip(left, right) if a > 1 and b > 1)
class Solution {
public int minimumMountainRemovals(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
left[i] = Math.max(left[i], left[j] + 1);
}
}
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] > nums[j]) {
right[i] = Math.max(right[i], right[j] + 1);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (left[i] > 1 && right[i] > 1) {
ans = Math.max(ans, left[i] + right[i] - 1);
}
}
return n - ans;
}
}
class Solution {
public:
int minimumMountainRemovals(vector<int>& nums) {
int n = nums.size();
vector<int> left(n, 1), right(n, 1);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
left[i] = max(left[i], left[j] + 1);
}
}
}
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
if (nums[i] > nums[j]) {
right[i] = max(right[i], right[j] + 1);
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (left[i] > 1 && right[i] > 1) {
ans = max(ans, left[i] + right[i] - 1);
}
}
return n - ans;
}
};
func minimumMountainRemovals(nums []int) int {
n := len(nums)
left, right := make([]int, n), make([]int, n)
for i := range left {
left[i], right[i] = 1, 1
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if nums[i] > nums[j] {
left[i] = max(left[i], left[j]+1)
}
}
}
for i := n - 2; i >= 0; i-- {
for j := i + 1; j < n; j++ {
if nums[i] > nums[j] {
right[i] = max(right[i], right[j]+1)
}
}
}
ans := 0
for i := range left {
if left[i] > 1 && right[i] > 1 {
ans = max(ans, left[i]+right[i]-1)
}
}
return n - ans
}
function minimumMountainRemovals(nums: number[]): number {
const n = nums.length;
const left = Array(n).fill(1);
const right = Array(n).fill(1);
for (let i = 1; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
left[i] = Math.max(left[i], left[j] + 1);
}
}
}
for (let i = n - 2; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
if (nums[i] > nums[j]) {
right[i] = Math.max(right[i], right[j] + 1);
}
}
}
let ans = 0;
for (let i = 0; i < n; ++i) {
if (left[i] > 1 && right[i] > 1) {
ans = Math.max(ans, left[i] + right[i] - 1);
}
}
return n - ans;
}
impl Solution {
pub fn minimum_mountain_removals(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut left = vec![1; n];
let mut right = vec![1; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] {
left[i] = left[i].max(left[j] + 1);
}
}
}
for i in (0..n - 1).rev() {
for j in i + 1..n {
if nums[i] > nums[j] {
right[i] = right[i].max(right[j] + 1);
}
}
}
let mut ans = 0;
for i in 0..n {
if left[i] > 1 && right[i] > 1 {
ans = ans.max(left[i] + right[i] - 1);
}
}
(n as i32) - ans
}
}