📌  相关文章
📜  最长子序列,使得相邻元素之间的差异是 A 或 B

📅  最后修改于: 2022-05-13 01:56:10.386000             🧑  作者: Mango

最长子序列,使得相邻元素之间的差异是 A 或 B

给定一个大小为N的数组arr和两个整数AB 。任务是找到最长子序列的长度,其中相邻元素之间的差异为AB

例子:

方法:仔细研究这个问题,这个问题类似于最长连续子序列。它们之间的唯一区别是现在我们必须计算具有差异AB而不是1的元素。现在,要解决此问题,请按照以下步骤操作:

  1. 创建一个映射,它将每个元素存储为键,并将以arr[i]结尾的最长子序列的长度作为值。
  2. 现在,遍历数组 arr,并为每个元素arr[i]
    • 在地图中搜索arr[i]-Aarr[i]+Aarr[i]-Barr[i]+B
    • 如果它们存在,则找到所有的最大值和+1以获得子序列的最大长度。
  3. 在地图中找到最大值,并将其作为答案返回

下面是上述方法的实现。

C++
// C++ code for the above approach
 
#include 
using namespace std;
 
// Function to find the length of
// longest common subsequence with
// difference between the consecutive
// element is either A or B
int maxSubsequence(vector& arr, int A, int B)
{
    int N = arr.size();
 
    int ans = 1;
 
    // Map to store the length of longest subsequence
    // ending with arr[i]
    unordered_map mp;
 
    for (int i = 0; i < N; ++i) {
        int aa = 1;
 
        // If arr[i]-A exists
        if (mp.count(arr[i] - A)) {
            aa = mp[arr[i] - A] + 1;
        }
 
        // If arr[i]+A exists
        if (mp.count(arr[i] + A)) {
            aa = max(aa, mp[arr[i] + A] + 1);
        }
 
        // If arr[i]-B exists
        if (mp.count(arr[i] - B)) {
            aa = max(aa, mp[arr[i] - B] + 1);
        }
 
        // If arr[i]+B exists
        if (mp.count(arr[i] + B)) {
            aa = max(aa, mp[arr[i] + B] + 1);
        }
 
        mp[arr[i]] = aa;
        ans = max(ans, mp[arr[i]]);
    }
 
    return ans;
}
 
// Driver Code
int main()
{
 
    vector arr = { 5, 5, 5, 10, 8, 6, 12, 13 };
    int A = 0, B = 1;
    cout << maxSubsequence(arr, A, B);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the length of
// longest common subsequence with
// difference between the consecutive
// element is either A or B
static int maxSubsequence(int []arr, int A, int B)
{
    int N = arr.length;
 
    int ans = 1;
 
    // Map to store the length of longest subsequence
    // ending with arr[i]
    HashMap mp = new HashMap();
 
    for (int i = 0; i < N; ++i) {
        int aa = 1;
 
        // If arr[i]-A exists
        if (mp.containsKey(arr[i] - A)) {
            aa = mp.get(arr[i] - A) + 1;
        }
 
        // If arr[i]+A exists
        if (mp.containsKey(arr[i] + A)) {
            aa = Math.max(aa, mp.get(arr[i] + A) + 1);
        }
 
        // If arr[i]-B exists
        if (mp.containsKey(arr[i] - B)) {
            aa = Math.max(aa, mp.get(arr[i] - B) + 1);
        }
 
        // If arr[i]+B exists
        if (mp.containsKey(arr[i] + B)) {
            aa = Math.max(aa, mp.get(arr[i] + B) + 1);
        }
        mp.put(arr[i], aa);
        ans = Math.max(ans, mp.get(arr[i]));
    }
 
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
 
    int []arr = { 5, 5, 5, 10, 8, 6, 12, 13 };
    int A = 0, B = 1;
    System.out.print(maxSubsequence(arr, A, B));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# python code for the above approach
 
# Function to find the length of
# longest common subsequence with
# difference between the consecutive
# element is either A or B
def maxSubsequence(arr, A, B):
    N = len(arr)
    ans = 1
 
    # Map to store the length of longest subsequence
    # ending with arr[i]
    mp = {}
 
    for i in range(0, N):
        aa = 1
 
        # If arr[i]-A exists
        if ((arr[i] - A) in mp):
            aa = mp[arr[i] - A] + 1
 
        # If arr[i]+A exists
        if ((arr[i] + A) in mp):
            aa = max(aa, mp[arr[i] + A] + 1)
 
        # If arr[i]-B exists
        if ((arr[i] - B) in mp):
            aa = max(aa, mp[arr[i] - B] + 1)
 
        # If arr[i]+B exists
        if ((arr[i] + B) in mp):
            aa = max(aa, mp[arr[i] + B] + 1)
 
        mp[arr[i]] = aa
        ans = max(ans, mp[arr[i]])
 
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    arr = [5, 5, 5, 10, 8, 6, 12, 13]
 
    A = 0
    B = 1
 
    print(maxSubsequence(arr, A, B))
 
    # This code is contributed by rakeshsahni


C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to find the length of
  // longest common subsequence with
  // difference between the consecutive
  // element is either A or B
  static int maxSubsequence(int[] arr, int A, int B)
  {
    int N = arr.Length;
 
    int ans = 1;
 
    // Map to store the length of longest subsequence
    // ending with arr[i]
    Dictionary mp
      = new Dictionary();
 
    for (int i = 0; i < N; ++i) {
      int aa = 1;
 
      // If arr[i]-A exists
      if (mp.ContainsKey(arr[i] - A)) {
        aa = mp[arr[i] - A] + 1;
      }
 
      // If arr[i]+A exists
      if (mp.ContainsKey(arr[i] + A)) {
        aa = Math.Max(aa, mp[arr[i] + A] + 1);
      }
 
      // If arr[i]-B exists
      if (mp.ContainsKey(arr[i] - B)) {
        aa = Math.Max(aa, mp[arr[i] - B] + 1);
      }
 
      // If arr[i]+B exists
      if (mp.ContainsKey(arr[i] + B)) {
        aa = Math.Max(aa, mp[arr[i] + B] + 1);
      }
      mp[arr[i]] = aa;
      ans = Math.Max(ans, mp[arr[i]]);
    }
 
    return ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    int[] arr = { 5, 5, 5, 10, 8, 6, 12, 13 };
    int A = 0, B = 1;
    Console.WriteLine(maxSubsequence(arr, A, B));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
4

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