📜  最长子序列,使得每对之间的绝对差最大为1

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

给定大小为N的整数数组arr [] ,任务是找到最长的子序列S ,使得对于每个a [i],a [j]∈。 S和| a [i] – a [j] | ≤1

例子:

建议:在继续解决方案之前,请先在“实践”上解决它。

方法:

想法是观察到,对于一个子序列,在每个可能的对之间存在差异的情况下,当子序列包含[X,X + 1]之间的元素时,一个子序列是最多可能的。

  • 将所需子序列的最大长度初始化为0。
  • 创建一个HashMap来存储数组中每个元素的频率。
  • 遍历Hash Map,并针对哈希图中的每个元素a [i] –
    • 查找元素(a [i] +1),(a [i])和(a [i] – 1)的出现次数。
    • 从元素(a [i] + 1)或(a [i] – 1)的出现中找到最大计数。
    • 如果出现的总数大于找到的最大长度,则更新子序列的最大长度。

下面是上述方法的实现。

Java
// Java implementation for 
// Longest subsequence such that absolute
// difference between every pair is atmost 1
  
import java.util.*;
public class GeeksForGeeks {
    public static int longestAr(
            int n, int arr[]){
        Hashtable count
            = new Hashtable();
  
        // Storing the frequency of each
        // element in the hashtable count
        for (int i = 0; i < n; i++) {
            if (count.containsKey(arr[i]))
                count.put(arr[i], count.get(
                    arr[i]) + 1
                );
            else
                count.put(arr[i], 1);
        }
  
        Set kset = count.keySet();
        Iterator it = kset.iterator();
  
        // Max is used to keep a track of
        // maximum length of the required 
        // subsequence so far.
        int max = 0;
  
        while (it.hasNext()) {
            int a = (int)it.next();
            int cur = 0;
            int cur1 = 0;
            int cur2 = 0;
  
            // Store frequency of the
            // given element+1.
            if (count.containsKey(a + 1))
                cur1 = count.get(a + 1);
  
            // Store frequency of the
            // given element-1.
            if (count.containsKey(a - 1))
                cur2 = count.get(a - 1);
  
            // cur store the longest array 
            // that can be formed using a.
            cur = count.get(a) +
                  Math.max(cur1, cur2);
  
            // update max if cur>max.
            if (cur > max)
                max = cur;
        }
  
        return (max);
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int n = 8;
        int arr[] = { 2, 2, 3, 5, 5, 6, 6, 6 };
        int maxLen = longestAr(n, arr);
        System.out.println(maxLen);
    }
}


Python3
# Python3 implementation for
# Longest subsequence such that absolute
# difference between every pair is atmost 1
  
def longestAr(n, arr):
    count = dict()
  
    # Storing the frequency of each
    # element in the hashtable count
    for i in arr:
        count[i] = count.get(i, 0) + 1
  
    kset = count.keys()
  
    # Max is used to keep a track of
    # maximum length of the required
    # subsequence so far.
    maxm = 0
  
    for it in list(kset):
        a = it
        cur = 0
        cur1 = 0
        cur2 = 0
  
        # Store frequency of the
        # given element+1.
        if ((a + 1) in count):
            cur1 = count[a + 1]
  
        # Store frequency of the
        # given element-1.
        if ((a - 1) in count):
            cur2 = count[a - 1]
  
        # cur store the longest array
        # that can be formed using a.
        cur = count[a] + max(cur1, cur2)
  
        # update maxm if cur>maxm.
        if (cur > maxm):
            maxm = cur
  
    return maxm
  
# Driver Code
if __name__ == '__main__':
    n = 8
    arr = [2, 2, 3, 5, 5, 6, 6, 6]
    maxLen = longestAr(n, arr)
    print(maxLen)
  
# This code is contributed by mohit kumar 29


输出:
5

时间复杂度: O(n)。
空间复杂度: O(n)。

如果您希望与行业专家一起参加实时课程,请参阅Geeks在线课程