📌  相关文章
📜  最大长度子序列,相邻元素之间的差为0或1

📅  最后修改于: 2021-04-27 06:04:57             🧑  作者: Mango

给定一个由n个整数组成的数组。问题在于找到子序列的最大长度,相邻元素之间的差为0或1。

例子:

Input : arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output : 5
The subsequence is {5, 6, 7, 6, 5}.

Input : arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output : 4
The subsequence is {-2, -1, -1, 0}.

资料来源: Expedia面试经验|套装12

该问题的解决方案与最长增加子序列问题非常相似。唯一的区别是在这里我们必须检查子序列的相邻元素之间的绝对差是0还是1。

C++
// C++ implementation to find maximum length
// subsequence with difference between adjacent 
// elements as either 0 or 1
#include 
using namespace std;
  
// function to find maximum length subsequence 
// with difference between adjacent elements as
// either 0 or 1
int maxLenSub(int arr[], int n)
{
    int mls[n], max = 0;
      
    // Initialize mls[] values for all indexes
    for (int i=0; iJava
// JAVA Code for Maximum length subsequence 
// with difference between adjacent elements
// as either 0 or 1
import java.util.*;
  
class GFG {
      
    // function to find maximum length subsequence 
    // with difference between adjacent elements as
    // either 0 or 1
     public static int maxLenSub(int arr[], int n)
    {
        int mls[] = new int[n], max = 0;
           
        // Initialize mls[] values for all indexes
        for (int i = 0; i < n; i++)
            mls[i] = 1;
           
        // Compute optimized maximum length 
        // subsequence values in bottom up manner
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (Math.abs(arr[i] - arr[j]) <= 1 
                      && mls[i] < mls[j] + 1)
                    mls[i] = mls[j] + 1;
           
        // Store maximum of all 'mls' values in 'max'    
        for (int i = 0; i < n; i++)
            if (max < mls[i])
                max = mls[i];
           
        // required maximum length subsequence
        return max;        
    }
      
    /* Driver program to test above function */
    public static void main(String[] args) 
    {
        int arr[] = {2, 5, 6, 3, 7, 6, 5, 8};
        int n = arr.length;
        System.out.println("Maximum length subsequence = "+
                               maxLenSub(arr, n));
            
    }
}
    
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python implementation to find maximum length
# subsequence with difference between adjacent 
# elements as either 0 or 1
  
# function to find maximum length subsequence 
# with difference between adjacent elements as
# either 0 or 1
def maxLenSub( arr, n):
    mls=[]
    max = 0
      
    #Initialize mls[] values for all indexes
    for i in range(n):
        mls.append(1)
      
    #Compute optimized maximum length subsequence
    # values in bottom up manner
    for i in range(n):
        for j in range(i):
            if (abs(arr[i] - arr[j]) <= 1 and mls[i] < mls[j] + 1):
                mls[i] = mls[j] + 1
                  
    # Store maximum of all 'mls' values in 'max'
    for i in range(n):
        if (max < mls[i]):
            max = mls[i]
      
    #required maximum length subsequence
    return max
      
#Driver program to test above
arr = [2, 5, 6, 3, 7, 6, 5, 8]
n = len(arr)
print("Maximum length subsequence = ",maxLenSub(arr, n))
  
#This code is contributed by "Abhishek Sharma 44"


C#
// C# Code for Maximum length subsequence
// with difference between adjacent elements
// as either 0 or 1
using System;
  
class GFG {
      
    // function to find maximum length subsequence
    // with difference between adjacent elements as
    // either 0 or 1
    public static int maxLenSub(int[] arr, int n)
    {
        int[] mls = new int[n];
        int max = 0;
  
        // Initialize mls[] values for all indexes
        for (int i = 0; i < n; i++)
            mls[i] = 1;
  
        // Compute optimized maximum length
        // subsequence values in bottom up manner
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (Math.Abs(arr[i] - arr[j]) <= 1
                    && mls[i] < mls[j] + 1)
                    mls[i] = mls[j] + 1;
  
        // Store maximum of all 'mls' values in 'max'
        for (int i = 0; i < n; i++)
            if (max < mls[i])
                max = mls[i];
  
        // required maximum length subsequence
        return max;
    }
  
    /* Driver program to test above function */
    public static void Main()
    {
        int[] arr = { 2, 5, 6, 3, 7, 6, 5, 8 };
        int n = arr.Length;
        Console.Write("Maximum length subsequence = " + 
                                    maxLenSub(arr, n));
    }
}
  
// This code is contributed by Sam007


PHP


输出:

Maximum length subsequence = 5

时间复杂度:O(n 2 )
辅助空间:O(n)

最大长度子序列,相邻元素之间的差为0或1 |套装2