数组排序最小复杂度_进行排序的最小缺失数

数组排序最小复杂度

Problem statement:

问题陈述:

Given an array of n integers. Find the minimum number of elements from the array to remove or delete so that when the remaining elements are placed in the same sequence order form a sorted sequence.

给定n个整数数组。 从数组中查找要删除或删除的最小元素数,以便在其余元素以相同顺序放置时形成排序的序列。

Input:

输入:

First line contains size N.
Next line contains N input elements for the array

第一行包含大小N。
下一行包含该数组的N个输入元素

Output:

输出:

Output the minimum number of deletions to make a sorted sequence.

输出删除的最小数量以构成排序序列。

Constraints:

限制条件:

1<= N <=1000
1<= A[i ] <=1000

Example:

例:

Input:
5
5 8 5 5 4

Output:
1

Explanation:
The longest increasing subsequence is: (not strictly increasing)
5, 8 or 5,5

So we need to remove minimum three characters
The longest decreasing subsequence is: (not strictly increasing)
8 5 5 4

So we need to remove minimum one character

Thus the final output is 1
And the sorted sequence is the decreasing one
8 5 5 4 

Solution Approach:

解决方法:

So, for the sequence to be sorted we need to check for both the longest increasing and decreasing subsequence.

因此,对于要排序的序列,我们需要检查最长的递增和递减的子序列。

Let,

让,

Longest increasing subsequence be known as LIS and Longest decreasing subsequence is LDS

最长的递增子序列称为LIS,最长的递减子序列为LDS

So minimum elements to be deleted= array length- maximum(LIS, LDS)

因此要删除的最小元素=数组长度-最大(LIS,LDS)

Intuitively, the minimum value of maximum(LIS, LDS) would be 1 as each element represents the primitive sequence which is either increasing or decreasing one.

直观地, 最大值(LIS,LDS)的最小值为1,因为每个元素代表原始序列,原始序列要么递增要么递减。

So, the base value is 1.

因此,基准值为1。

Now,

现在,

Lis(i)=longest increasing subsequence starting from index 0 to index i
Lds(i)=longest decreasing subsequence starting from index 0 to index i 

So,

所以,

To compute LIS(i), LDS(i) the recursion function is,

为了计算LIS(i),LDS(i) ,递归函数为

Minimum number of deletions to make a sorted sequence

As, the base value is 1, for every index i, Lis(i), Lds(i) is at least 1.

这样,对于每个索引i ,基值是1, Lis(i)Lds(i)至少为1。

1)  Create two DP array, Lis[n],Lds[n]
2)  Initialize both the DP array with 1.
        for i=0 to n-1
            Lis[i]=1,Lds[i]=1;
3)  Now, to compute the Lis[i],Lds[i]
        for index  i=1 to n-1         
            for previous index j=0 to i-1
                //if (arr[i],arr[j]) is inceasing sequence
                if(lis[i]<lis[j]+1 &&a[i]≥a[j])
                    lis[i]=lis[j]+1;
                //if (arr[i],arr[j]) is deceasing sequence
                if(lds[i]<lds[j]+1 &&a[i]≤a[j])
                    lds[i]=lds[j]+1;
            end for
        end for 

Now, Minimum elements to be deleted =

现在,要删除的最少元素=

n-maximum(maximum value in (lds),maximum value in (lis))

To go through detailed explanation on LIS go through previous article on LIS: Longest Increasing Subsequence

要详细了解LIS,请阅读上一篇有关LIS: 最长递增子序列的文章。

LDS is quite similar like LIS, follow the recursion for LDS to understand this too.

LDS与LIS十分相似,也遵循LDS的递归来理解这一点。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int LIDS(vector<int> arr, int n)
{

    int LIS[n], LDS[n];

    for (int i = 0; i < n; i++)
        LIS[i] = 1;
    for (int i = 0; i < n; i++)
        LDS[i] = 1;

    int maxi = INT_MIN, maxd = INT_MIN;

    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            // for longest increasing sequence
            if (arr[i] >= arr[j] && LIS[i] < LIS[j] + 1)
                LIS[i] = LIS[j] + 1;

            // for longest decreasing sequence
            if (arr[i] <= arr[j] && LDS[i] < LDS[j] + 1)
                LDS[i] = LDS[j] + 1;
        }
        //find maximum longest s orted sequence
        if (LIS[i] > maxi)
            maxi = LIS[i];

        if (LDS[i] > maxd)
            maxd = LDS[i];
    }

    return std::max(maxi, maxd);
}

int main()
{
    int t, n, item;
    cout << "Enter n:\n";
    scanf("%d", &n);
    cout << "Enter the array\n";
    vector<int> a;
    for (int j = 0; j < n; j++) {
        scanf("%d", &item);
        a.push_back(item);
    }

    cout << "Minimum elements needed to be deleted to create sorted sequence: " << n - LIDS(a, n) << endl;

    return 0;
}

Output:

输出:

Enter n:
5
Enter the array
5 8 5 5 4
Minimum elements needed to be deleted to create sorted sequence: 1


翻译自: https://www.includehelp.com/icp/minimum-number-of-deletions-to-make-a-sorted-sequence.aspx

数组排序最小复杂度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值