给定一个大小为N的数组A[] ,任务是通过将元素从给定数组的任一端追加到数组的末尾,找到可以从给定数组生成的最长非递减数组的最大长度结果数组并一一删除附加元素。
例子:
Input: A[] = {5, 6, 8, 7}
Output:: 4
Explanation:
If resArr[] = { } is a newly generated array then
Inserting A[0] into resArr[] modifies A[] = {6, 8, 7} and resArr[] = {5}
Inserting A[0] into resArr[] modifies A[] = {8, 7} and resArr[] = {5, 6}
Inserting A[1] into resArr[] modifies A[] = {8} and resArr[] = {5, 6, 7}
Inserting A[0] into resArr[] modifies A[] = {} and resArr[] = {5, 6, 7, 8}
Therefore, the length of the longest non-decreasing array that can be generated from the given array is 4.
Input: {1, 1, 3, 5, 4, 3, 6, 2, 1}
Output: 7
方法:这个问题可以使用两个指针的方法来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如res = 0来存储可以从给定数组生成的非递减数组的最大长度。
- 初始化两个变量,比如start = 0和end = N – 1来存储开始和结束指针的索引。
- 遍历数组并检查以下条件:
- 如果A[start] <= A[end]则检查A[start]的值是否大于或等于新数组中先前插入的元素。如果发现为真,则将start和res的值增加1 。
- 如果A [开始]> = A [结束]然后检查是否A [结束]的值是大于或等于所述新的数组或没有在先前插入的元件。如果发现为真,则将end的值减少1并将res的值增加1 。
- 最后,打印res的值。
下面是上述方法的实现:
C++
//C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the length of the longest
// non-decreasing array that can be generated
int findLongestNonDecreasing(int A[], int N)
{
// Stores the length of the longest
// non-decreasing array that can be
// generated from the array
int res = 0;
// Stores index of
// start pointer
int start = 0;
// Stores index of
// end pointer
int end = N - 1;
// Stores previously inserted
// element into the new array
int prev = -1;
// Traverse the array
while (start <= end) {
// If A[start] is less than
// or equal to A[end]
if (A[start] <= A[end]) {
// If no element inserted into
// the newly generated array
if (prev == -1) {
// Update prev
prev = A[start];
// Update res
res++;
// Update start
start++;
}
else {
// If A[start] is greater
// than or equal to prev
if (A[start] >= prev) {
// Update res
res++;
// Update prev
prev = A[start];
// Update start
start++;
}
// If A[end] is greater
// than or equal to prev
else if (A[end] >= prev) {
// Update res
res++;
// Update prev
prev = A[end];
// Update end
end--;
}
else {
break;
}
}
}
// If A[end] is
// greater than A[start]
else{
// If no element inserted into
// the newly generated array
if (prev == -1) {
// Update prev
prev = A[end];
// Update res
res++;
// Update end
end--;
}
else {
// If A[end] is greater
// than or equal to prev
if (A[end] >= prev) {
// Update res
res++;
//Update prev
prev = A[end];
// Update end
end--;
}
// If A[start] is greater
// than or equal to prev
else if (A[start] >= prev) {
// Update res
res++;
//Update prev
prev = A[start];
// Update start
start++;
}
else {
break;
}
}
}
}
return res;
}
//Driver Code
int main()
{
int A[]={ 1, 1, 3, 5, 4, 3, 6, 2, 1 };
int N = sizeof(A)/sizeof(A[0]);
//Function call
cout<< findLongestNonDecreasing(A, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the length of the longest
// non-decreasing array that can be generated
static int findLongestNonDecreasing(int A[],
int N)
{
// Stores the length of the longest
// non-decreasing array that can be
// generated from the array
int res = 0;
// Stores index of
// start pointer
int start = 0;
// Stores index of
// end pointer
int end = N - 1;
// Stores previously inserted
// element into the new array
int prev = -1;
// Traverse the array
while (start <= end)
{
// If A[start] is less than
// or equal to A[end]
if (A[start] <= A[end])
{
// If no element inserted into
// the newly generated array
if (prev == -1)
{
// Update prev
prev = A[start];
// Update res
res++;
// Update start
start++;
}
else
{
// If A[start] is greater
// than or equal to prev
if (A[start] >= prev)
{
// Update res
res++;
// Update prev
prev = A[start];
// Update start
start++;
}
// If A[end] is greater
// than or equal to prev
else if (A[end] >= prev)
{
// Update res
res++;
// Update prev
prev = A[end];
// Update end
end--;
}
else
{
break;
}
}
}
// If A[end] is
// greater than A[start]
else
{
// If no element inserted into
// the newly generated array
if (prev == -1)
{
// Update prev
prev = A[end];
// Update res
res++;
// Update end
end--;
}
else
{
// If A[end] is greater
// than or equal to prev
if (A[end] >= prev)
{
// Update res
res++;
//Update prev
prev = A[end];
// Update end
end--;
}
// If A[start] is greater
// than or equal to prev
else if (A[start] >= prev)
{
// Update res
res++;
//Update prev
prev = A[start];
// Update start
start++;
}
else
{
break;
}
}
}
}
return res;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 1, 1, 3, 5, 4, 3, 6, 2, 1 };
int N = A.length;
// Function call
System.out.print(findLongestNonDecreasing(A, N));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
# Function to find the length of
# the longest non-decreasing array
# that can be generated
def findLongestNonDecreasing(A, N):
# Stores the length of the longest
# non-decreasing array that can be
# generated from the array
res = 0;
# Stores index of
# start pointer
start = 0;
# Stores index of
# end pointer
end = N - 1;
# Stores previously inserted
# element into the new array
prev = -1;
# Traverse the array
while (start <= end):
# If A[start] is less than
# or equal to A[end]
if (A[start] <= A[end]):
# If no element inserted into
# the newly generated array
if (prev == -1):
# Update prev
prev = A[start];
# Update res
res += 1
# Update start
start += 1
else:
# If A[start] is greater
# than or equal to prev
if (A[start] >= prev):
# Update res
res += 1
# Update prev
prev = A[start];
# Update start
start += 1
# If A[end] is greater
# than or equal to prev
elif (A[end] >= prev):
# Update res
res += 1
# Update prev
prev = A[end];
# Update end
end -= 1
else:
break;
# If A[end] is
# greater than A[start]
else:
# If no element inserted into
# the newly generated array
if (prev == -1):
# Update prev
prev = A[end];
# Update res
res += 1
# Update end
end -= 1
else:
# If A[end] is greater
# than or equal to prev
if (A[end] >= prev):
# Update res
res += 1
# Update prev
prev = A[end];
# Update end
end -= 1
# If A[start] is greater
# than or equal to prev
elif (A[start] >= prev):
# Update res
res += 1
# Update prev
prev = A[start];
# Update start
start += 1
else :
break;
return res
# Driver Code
if __name__ == "__main__":
A = [1, 1, 3, 5,
4, 3, 6, 2, 1]
N = len(A)
# Function call
print (findLongestNonDecreasing(A, N));
# This code is contributed by Chitranayal
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the length of the longest
// non-decreasing array that can be generated
static int findLongestNonDecreasing(int[] A,
int N)
{
// Stores the length of the longest
// non-decreasing array that can be
// generated from the array
int res = 0;
// Stores index of
// start pointer
int start = 0;
// Stores index of
// end pointer
int end = N - 1;
// Stores previously inserted
// element into the new array
int prev = -1;
// Traverse the array
while (start <= end)
{
// If A[start] is less than
// or equal to A[end]
if (A[start] <= A[end])
{
// If no element inserted into
// the newly generated array
if (prev == -1)
{
// Update prev
prev = A[start];
// Update res
res++;
// Update start
start++;
}
else
{
// If A[start] is greater
// than or equal to prev
if (A[start] >= prev)
{
// Update res
res++;
// Update prev
prev = A[start];
// Update start
start++;
}
// If A[end] is greater
// than or equal to prev
else if (A[end] >= prev)
{
// Update res
res++;
// Update prev
prev = A[end];
// Update end
end--;
}
else
{
break;
}
}
}
// If A[end] is
// greater than A[start]
else
{
// If no element inserted into
// the newly generated array
if (prev == -1)
{
// Update prev
prev = A[end];
// Update res
res++;
// Update end
end--;
}
else
{
// If A[end] is greater
// than or equal to prev
if (A[end] >= prev)
{
// Update res
res++;
//Update prev
prev = A[end];
// Update end
end--;
}
// If A[start] is greater
// than or equal to prev
else if (A[start] >= prev)
{
// Update res
res++;
//Update prev
prev = A[start];
// Update start
start++;
}
else
{
break;
}
}
}
}
return res;
}
// Driver Code
public static void Main()
{
int[] A = { 1, 1, 3, 5, 4, 3, 6, 2, 1 };
int N = A.Length;
// Function call
Console.Write(findLongestNonDecreasing(A, N));
}
}
// This code is contributed by sanjoy_62
Javascript
7
时间复杂度: O(N)
空间复杂度: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live