數組排序最小復雜度_進行排序的最小缺失數

數組排序最小復雜度

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.pswp.cn/news/379882.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379882.shtml
英文地址,請注明出處:http://en.pswp.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;你一定會…