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

数组排序最小复杂度

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

数组排序最小复杂度

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379882.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

轻松掌握Windows窗体间的数据交互(转载)

轻松掌握Windows窗体间的数据交互作者&#xff1a;郑佐日期&#xff1a;2004-04-05Windows 窗体是用于 Microsoft Windows 应用程序开发的、基于 .NET Framework 的新平台。此框架提供一个有条理的、面向对象的、可扩展的类集&#xff0c;它使您得以开发丰富的 Windows 应用程序…

MATLAB安装问题解决方案大集锦

我的安装后的两个问题 第一个&#xff1a;“Microsoft Visual C Runtime LibraryRuntime Error!Program:C:\Matlab7\Rin\Win32\Matlab.exeThis application has requested the runtime to terminate it in an unusual way.Please contact the applications support team for mo…

python免杀技术---shellcode的加载与执行

0x01 生成shellcode 首先通过下列命令生成一个shellcode&#xff0c;使用msfvenom -p选项来指定paylaod&#xff0c;这里选用windows/x64、exec模块接收的参数。使用calc.exe执行弹出计算器的操作。-f选项用来执行生成的shellcdoe的编译语言。 msfvenom -p windows/x64/exec …

成对的歌曲,其总持续时间可被60整除

Problem statement: 问题陈述&#xff1a; In a list of songs, the i-th song has duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j w…

Qt中QTableWidget用法总结

QTableWidget是QT程序中常用的显示数据表格的空间&#xff0c;很类似于VC、C#中的DataGrid。说到QTableWidget&#xff0c;就必须讲一下它跟QTabelView的区别了。QTableWidget是QTableView的子类&#xff0c;主要的区别是QTableView可以使用自定义的数据模型来显示内容(也就是先…

[转]软件架构师书单

"其实中国程序员&#xff0c;现在最需要的是一张安静的书桌。"&#xff0c;的确&#xff0c;中国架构师大多缺乏系统的基础知识&#xff0c;与其自欺欺人的宣扬"读书无用&#xff0c;重在实践变通&#xff0c;修身立命哲学书更重要"&#xff0c;把大好时间…

Java——List集合特有的功能

* List也是一个接口&#xff0c;这说明List不能new&#xff0c;其中它有一个子类ArrayList&#xff0c;所以&#xff0c;就可以父类引用指向子类对象调用* List里面特有的方法&#xff1a;* * void add(int index,E element)在列表的指定位置插入指定元素&#xff08;可选操作&…

python免杀技术---复现+改进----1

0x01 复现 复现文章&#xff1a;https://mp.weixin.qq.com/s?__bizMzI3MzUwMTQwNg&mid2247484733&idx2&sn5b8f439c2998ce089eb44541d2da7a15&chksmeb231%E2%80%A6 首先用cobaltstruke生成一个python的payload脚本 然后复制里面的payload进行Base64编码&…

python掷骰子_用于掷骰子的Python程序(2人骰子游戏)

python掷骰子Here, we will be going to design a very simple and easy game and implement it using abstract data class. The code consists of two different classes (The base of the whole program). The one will be the class for the player and others will be for…

ForeignKey和ManyToManyField的限制关系

authorsmodels.ManyToManyField(Author,limit_choice_to{name__endswith:Smith}这样可以更方便的查询。转载于:https://www.cnblogs.com/chenjianhong/archive/2012/03/22/4145158.html

linux 目录命令_Linux目录命令能力问题和解答

linux 目录命令This section contains Aptitude Questions and Answers on Linux Directory Commands. 本节包含有关Linux目录命令的 Aptitude问答。 1) There are the following statements that are given which of them are correct about Linux commands? In the Linux o…

终于在HP2133上成功安装xp

今天拿到一台HP2133迷你笔记本&#xff0c;原装vista home basic&#xff0c;由于本人是在不喜欢vista&#xff0c;于是决定将使用xp换之。 很久没有研究装系统了&#xff0c;HP2133没有光驱&#xff0c;以前也没啥这方面经验&#xff0c;搞这个玩意安装完软件折腾了大半天&…

Java——GUI(图形用户界面设计)

事件处理&#xff1a;事件&#xff1a;用户的一个操作(例如&#xff1a;点击一下鼠标&#xff0c;或者敲击一下键盘)事件源&#xff1a;被操作的组件(例如&#xff1a;在一个窗体中的一个按钮&#xff0c;那个按钮就属于被操作的组件&#xff0c;按钮就是事件源)监听器&#xf…

python安全攻防---信息收集---IP查询

IP查询是通过当前所获得的URL去查询对应IP地址的过程&#xff0c;可应用Socket库函数中的gethostbyname()获取域名所对用的IP值 程序如下&#xff1a; # -*- coding:utf-8 -*- IP查询import socket ip socket.gethostbyname(www.baidu.com) print(ip)运行结果&#xff1a; …

智能课程表Android版-学年学期星期的实现

上次我们实现了日期和时间的动态显示&#xff0c;这次我们来实现学年&#xff0c;学期&#xff0c;周次的显示&#xff0c;如图: 首先是学年学期的显示&#xff1a; Calendar cCalendar.getInstance(); int yearc.get(Calendar.YEAR); int monthc.get(Calendar.MONTH)1;//Calen…

感染linux脚本程序技术

前言 ---- 本文来源于29A病毒杂志,其上对linux shell病毒技术有了一个综合的阐述,我不想翻译它,我以它的那篇为模板 写了这篇中文的文章,里面的代码我都做了调试. 对于shell编程的程序员来说所谓的shell病毒技术其实根本就是小牛一毛,这点在大家看完本文后就会有所体会 但,简单…

Java——设计模式(简单工厂模式)

* A:简单工厂模式概述* 简单工厂模式又叫静态工厂方法模式&#xff0c;它定义了一个具体的工厂类负责创建一些类的实例* B&#xff1a;优点* 客户端不需要再负责对象的创建&#xff0c;从而明确了各个类的职责* 简单来说&#xff0c;客户端你只需要用就可以了&#xff0c;就…

Java ObjectOutputStream writeFloat()方法与示例

ObjectOutputStream类writeFloat()方法 (ObjectOutputStream Class writeFloat() method) writeFloat() method is available in java.io package. 在java.io包中提供了writeFloat()方法 。 writeFloat() method is used to write the given 4 bytes of a float value. writeFl…

python安全攻防---信息收集---whois查询

whois是用来查询域名的IP以及所有者信息的传输协议。简单地说&#xff0c;whois就是一个数据库&#xff0c;用来查询域名是否以及被注册&#xff0c;以及注册域名的详细信息&#xff08;如域名所有人、域名注册商等&#xff09;。 使用whois查询&#xff0c;首先通过pip安装py…

百度面试题:从输入url到显示网页,后台发生了什么?

参考http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/wenanry/archive/2010/02/25/1673368.html 原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ 作为一个软件开发者&#xff0c;你一定会…