📌  相关文章
📜  通过给定数组两端存在的元素,使最长的非递减数组的长度最大化

📅  最后修改于: 2021-05-17 18:05:38             🧑  作者: Mango

给定大小为N的数组A [] ,任务是找到可通过将给定数组两端的元素附加到数组末尾来从给定数组生成的最长非递减数组的最大长度。结果数组并一一删除附加元素。

例子:

方法:可以使用两个指针方法解决此问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如res = 0,以存储可以从给定数组生成的非递减数组的最大长度。
  • 初始化两个变量,例如start = 0end = N – 1,以存储开始和结束指针的索引。
  • 遍历数组并检查以下条件:
    • 如果A [start] <= A [end],则检查A [start]的值是否大于或等于新数组中先前插入的元素。如果确定为true,则将startres的值增加1
    • 如果A [开始]> = A [结束]然后检查是否A [结束]的值是大于或等于所述新的数组或没有在先前插入的元件。如果确定为true,则将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)